Custom back button for NavigationView's navigation bar in SwiftUI

后端 未结 9 634
隐瞒了意图╮
隐瞒了意图╮ 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:41

    TL;DR

    Use this to transition to your view:

    NavigationLink(destination: SampleDetails()) {}
    

    Add this to the view itself:

    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    

    Then, in a button action or something, dismiss the view:

    presentationMode.wrappedValue.dismiss()
    

    Full code

    From a parent, navigate using NavigationLink

     NavigationLink(destination: SampleDetails()) {}
    

    In DetailsView hide navigationBarBackButton and set custom back button to leading navigationBatItem,

    struct SampleDetails: View {
        @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    
        var btnBack : some View { Button(action: {
            self.presentationMode.wrappedValue.dismiss()
            }) {
                HStack {
                Image("ic_back") // set image here
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.white)
                    Text("Go back")
                }
            }
        }
    
        var body: some View {
                List {
                    Text("sample code")
            }
            .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading: btnBack)
        }
    }
    
    0 讨论(0)
  • 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<PresentationMode>
    
        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
        }
    }
    
    0 讨论(0)
  • 2020-11-30 00:44

    All of the solutions I see here seem to disable swipe to go back functionality to navigate to the previous page, so sharing a solution I found that maintains that functionality. You can make an extension of your root view and override your navigation style and call the function in the view initializer.

    Sample View

    struct SampleRootView: View {
    
        init() {
            overrideNavigationAppearance()
        }
    
        var body: some View {
            Text("Hello, World!")
        }
    }
    

    Extension

    extension SampleRootView {
       func overrideNavigationAppearance() {
            let navigationBarAppearance = UINavigationBarAppearance()
            let barAppearace = UINavigationBar.appearance()
            barAppearace.tintColor = *desired UIColor for icon*
            barAppearace.barTintColor = *desired UIColor for icon*
    
            navigationBarAppearance.setBackIndicatorImage(*desired UIImage for custom icon*, transitionMaskImage: *desired UIImage for custom icon*)
    
            UINavigationBar.appearance().standardAppearance = navigationBarAppearance
            UINavigationBar.appearance().compactAppearance = navigationBarAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
       }
    }
    

    The only downfall to this approach is I haven't found a way to remove/change the text associated with the custom back button.

    0 讨论(0)
提交回复
热议问题