SwiftUI - Navigation bar colour change not being applied to status bar

时光毁灭记忆、已成空白 提交于 2021-01-28 11:57:47

问题


I have the following:

var body: some View {
        NavigationView {
            VStack {
                 Text("Hello")
            }.navigationBarTitle("Edit Profile", displayMode: .inline)
             .background(NavigationConfiguration { nc in
                 nc.navigationBar.barTintColor = .red
                 nc.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.black]
            })
        }.navigationViewStyle(StackNavigationViewStyle())
}

For the configuration of the nav bar i have:

struct NavigationConfiguration: UIViewControllerRepresentable {
    var configuration: (UINavigationController) -> Void = { _ in }
    func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfiguration>) -> UIViewController {
        UIViewController()
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfiguration>) {
        if let nc = uiViewController.navigationController {
            self.configuration(nc)
        }
    }
}

But for some reason the top status bar is being left out and the colour is not being applied to it:

How can the red colour also be applied to the status bar above the nav bar, I want it to be the same colour.

I've tried the below, but this fails:

init() {
    UINavigationBar.appearance().backgroundColor = .red
}

回答1:


Try the following (it should work, at least as tested with Xcode 11.4 / iOS 13.4, but someone reported it was not, so it might be dependent)

init() {
       let navBarAppearance = UINavigationBarAppearance()
       navBarAppearance.configureWithOpaqueBackground()
       navBarAppearance.backgroundColor = UIColor.systemRed

       UINavigationBar.appearance().standardAppearance = navBarAppearance
}



回答2:


Try using this modifier as this gives you freedom to change color based on each view.

struct NavigationBarModifier: ViewModifier {

var backgroundColor: UIColor = .clear

init(backgroundColor: UIColor, tintColor: UIColor = .white) {
    self.backgroundColor = backgroundColor

    let coloredAppearance = UINavigationBarAppearance()
    coloredAppearance.backgroundColor = backgroundColor
    coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]

    UINavigationBar.appearance().standardAppearance = coloredAppearance
    UINavigationBar.appearance().compactAppearance = coloredAppearance
    UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
    UINavigationBar.appearance().tintColor = tintColor
}

func body(content: Content) -> some View {
    ZStack{
        content
        VStack {
            GeometryReader { geometry in
                Color(self.backgroundColor)
                    .frame(height: geometry.safeAreaInsets.top)
                    .edgesIgnoringSafeArea(.top)
                Spacer()
            }
        }
    }
}}

I used it on below View:

struct DummyNavigationView: View {
var body: some View {
    NavigationView {
        VStack {
            NavigationLink(destination: Text("Page 2")) {
                Text("Go to detail")
            }
        }
        .navigationBarTitle("Edit Profile", displayMode: .inline)
    }
    .modifier(NavigationBarModifier(backgroundColor: .red, tintColor: .black))
}}


来源:https://stackoverflow.com/questions/62114018/swiftui-navigation-bar-colour-change-not-being-applied-to-status-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!