Home > AI > IOS > UIKit >

UINavigationBar

Example 1:
downsides: literately all navigation bar backgrounds get changed since this method is static.

struct PlayerView: View {
    
    init() {
        UINavigationBar.appearance().backgroundColor = .blue
    }
    
    var body: some View {
        NavigationView {
            NavigationLink(destination:
                            Text("good")) {
                    Text("Go to first detail")
            }
        }
    }
}

Example 2: Different navigation views in TabView get affected.

struct PlayerView: View {
    
    var body: some View {
        TabView {
            FirstTabView()
                .tabItem { Text("First") }
                .tag(0)
            SecondTabView()
                .tabItem { Text("Second") }
                .tag(1)
        }
    }
}


struct FirstTabView: View {
 
    init() {
        UINavigationBar.appearance().backgroundColor = .blue
    }
    
    var body: some View {
        NavigationView {
            Text("First detail")
        }
        
    }
}


struct SecondTabView: View {
 
    var body: some View {
        NavigationView {
            Text("Second detail")
        }
        
    }
}

Example 3: extend UINavigationController, but it is still global

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()
        
        let appearance = UINavigationBarAppearance()
        navigationBar.standardAppearance = appearance
        navigationBar.backgroundColor = .blue
    }
}

Leave a Reply