Custom back button for NavigationView's navigation bar in SwiftUI

后端 未结 9 713
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 23:57

I want to add a custom navigation button that will look somewhat like this:

Now, I\'ve written a custom BackButton view for this. When applying

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 00:42

    Based on other answers here, this is a simplified answer for Option 2 working for me in XCode 11.0:

    struct DetailView: View {
        @Environment(\.presentationMode) var presentationMode: Binding
    
        var body: some View {
    
            Button(action: {
               self.presentationMode.wrappedValue.dismiss()
            }) {
                Image(systemName: "gobackward").padding()
            }
            .navigationBarHidden(true)
    
        }
    }
    

    Note: To get the NavigationBar to be hidden, I also needed to set and then hide the NavigationBar in ContentView.

    struct ContentView: View {
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(destination: DetailView()) {
                        Text("Link").padding()
                    }
                } // Main VStack
                .navigationBarTitle("Home")
                .navigationBarHidden(true)
    
            } //NavigationView
        }
    }
    

提交回复
热议问题