How to change color of back button on NavigationView

后端 未结 6 1588
情书的邮戳
情书的邮戳 2021-01-02 04:56

When you click on the button it takes you to a new view and puts a back button in the top left. I can\'t figure out what property controls the color of the back button. I tr

相关标签:
6条回答
  • 2021-01-02 05:06

    I doubt this is the right way to do it, but I got it to work by modifying the SceneDelegate.swift to set the window tint color.

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
        // Use a UIHostingController as window root view controller
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIHostingController(rootView: ContentView())
        window.tintColor = .green // set the colour of the back navigation text
        self.window = window
        window.makeKeyAndVisible()
    }
    
    0 讨论(0)
  • 2021-01-02 05:18

    You could try setting the foregroundColor or the accentColor to the NavigationView (after your second to last closing bracket)

    0 讨论(0)
  • 2021-01-02 05:20
    
    .navigationBarBackButtonHidden(true)
            .navigationBarItems(leading:
                    Button(action: {presentation.wrappedValue.dismiss()}, label: {
                        HStack {
                        Image(systemName: "chevron.backward")
                            .foregroundColor(Color(UIColor.darkGray))
                        Text(username)
                            .foregroundColor(Color(UIColor.darkGray))
                            .font(UIHelper.largeSize())
                        }
                    })
    
    0 讨论(0)
  • 2021-01-02 05:21

    You can use the accentColor property on the NavigationView to set the back button color, like in this example:

    var body: some View {
        NavigationView {
            List(1..<13) { item in
                NavigationLink(destination: Text("\(item) x 8 = \(item*8)")) {
                    Text(String(item))
                }
            }.navigationBarTitle("Table of 8")
        }.accentColor( .black) // <- note that it's added here and not on the List like navigationBarTitle()
    }
    
    0 讨论(0)
  • 2021-01-02 05:21

    This worked for me:

    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    
    0 讨论(0)
  • 2021-01-02 05:23

    I was trying to do the same for a while, and do not think there is SwiftUI solution yet. One of things that will get work done though (if it works for your use case) is UIKit's appearance:

    UINavigationBar.appearance().tintColor = .black

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