Home > Enterprise >  How to change the color of navigationTitle for only one view?
How to change the color of navigationTitle for only one view?

Time:08-04

I want to change to color of the navigationTitle of View1 to green, without changing the color of the navigationTitle of View2. I looked at this question and achieved to change the color at all, but this is not quite what I need. Is there a way in SwiftUI to change only the color of the navigationTitle for a specific view and not the whole NavigationView?

Here is my code:

struct View1: View {
    // copied from the question I mentioned above
    init() {
        let navbarAppearance = UINavigationBarAppearance()
        navbarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.green]
        navbarAppearance.titleTextAttributes = [.foregroundColor: UIColor.green]
        navbarAppearance.configureWithTransparentBackground()
        UINavigationBar.appearance().standardAppearance = navbarAppearance
        UINavigationBar.appearance().compactAppearance = navbarAppearance
        UINavigationBar.appearance().scrollEdgeAppearance = navbarAppearance
    }

    var body: some View {
        NavigationView{
            NavigationLink(
                destination: View2())
            {
                Text("go to view 2")
            }
            .navigationTitle("View1")
        }
    }
}

struct View2: View {
    var body: some View {
        Text("view2")
            .navigationTitle("View2")
    }
}

Any help is appreciated, thanks a lot!

CodePudding user response:

This link may be helpful. There is solution:

struct View1: View {
    var body: some View {
        NavigationView{
            ZStack {
                NavigationControllerAccessor(viewWillAppear: { nav in
                    nav.navigationBar.largeTitleTextAttributes = [
                        .foregroundColor: UIColor.green
                    ]
                }, viewWillDisappear: { nav in
                    nav.navigationBar.largeTitleTextAttributes = nil
                })
                
                NavigationLink(
                    destination: View2())
                {
                    Text("go to view 2")
                }
            }
            .navigationTitle("View1")
            
        }
        // This is important, it doesn't work without it.
        .navigationViewStyle(.stack)
    }
}

struct View2: View {
    var body: some View {
        Text("view2")
            .navigationTitle("View2")
    }
}

struct NavigationControllerAccessor: UIViewControllerRepresentable {
    var viewWillAppear: (UINavigationController) -> Void
    var viewWillDisappear: ((UINavigationController) -> Void)? = nil
    
    private let proxyController = ViewController()
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationControllerAccessor>) -> UIViewController {
        proxyController.viewWillAppearCallback = viewWillAppear
        if let viewWillDisappear = viewWillDisappear {
            proxyController.viewWillDisappearCallback = viewWillDisappear
        }
        return proxyController
    }
    
    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationControllerAccessor>) {}
    
    private class ViewController: UIViewController {
        var viewWillAppearCallback: (UINavigationController) -> Void = { _ in }
        var viewWillDisappearCallback: (UINavigationController) -> Void = { _ in }
        
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            if let nav = navigationController {
                viewWillAppearCallback(nav)
            }
        }
        
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            if let nav = navigationController {
                viewWillDisappearCallback(nav)
            }
        }
    }
}
  • Related