How to wrap a UIBarButtonItem in a SwiftUI View?

后端 未结 1 1988
遇见更好的自我
遇见更好的自我 2021-01-26 14:28

I\'m trying to make a custom back button for the NavigationBar where I can put whatever text I like. My primary use case is that some of my views have no title, and

1条回答
  •  悲哀的现实
    2021-01-26 15:26

    If you're just trying to have custom text, then no need to bother with UIKit. Here's how you can make a custom back button in SwiftUI:

    struct ViewWithCustomBackButton: View {
        @Environment(\.presentationMode) var presentationMode
    
        var body: some View {
            HStack {
                ...
            }
            .navigationBarTitle(Text("Your View"), displayMode: .inline)
            // Hide the system back button
            .navigationBarBackButtonHidden(true)
            // Add your custom back button here
            .navigationBarItems(leading:
                Button(action: {
                    self.presentationMode.wrappedValue.dismiss()
                }) {
                    HStack {
                        Image(systemName: "arrow.left.circle")
                        Text("Go Back")
                    }
            })
        }
    }
    

    You can also check out the answers here for more info: Custom back button for NavigationView's navigation bar in SwiftUI

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