Skip to content

Instantly share code, notes, and snippets.

@gbuela
Created February 9, 2020 03:14
Show Gist options
  • Save gbuela/a2fee09d8f83477b475d52242b3d835f to your computer and use it in GitHub Desktop.
Save gbuela/a2fee09d8f83477b475d52242b3d835f to your computer and use it in GitHub Desktop.
Custom navigation bar in SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.black
.edgesIgnoringSafeArea([.all])
NavigationLink(destination: ContentView2()) {
Text("push")
}
}
}
}
}
struct ContentView2: View {
var body: some View {
NavView(title: "My custom bar", content:
ZStack {
Color.black
.edgesIgnoringSafeArea([.all])
NavigationLink(destination: ContentView2()) {
Text("push")
}
}
)
}
}
struct NavView<Content>: View where Content: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
let title: String
let content: Content
var body: some View {
NavigationView {
VStack {
NavBar(backHandler: { self.presentationMode.wrappedValue.dismiss() }, title: title)
content
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
.navigationBarTitle("")
.navigationBarHidden(true)
}
}
struct NavBar: View {
let backHandler: (() -> Void)
let title: String
var body: some View {
VStack {
Spacer()
HStack {
Button(action: { self.backHandler() }) {
HStack {
Image(systemName: "chevron.left")
Text("Back")
.font(.system(size: 16))
}.foregroundColor(Color.blue)
}
Spacer()
Text(title)
.font(.system(size: 16))
.lineLimit(1)
.foregroundColor(Color.white)
Spacer()
}.padding([.leading, .trailing], 16)
Divider()
Spacer()
}
.background(Color.black.edgesIgnoringSafeArea(.all))
.frame(height: 45)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment