SwiftUI-导航栏颜色更改未应用于状态栏

时间:2020-05-31 09:16:51

标签: swift uinavigationcontroller swiftui uinavigationbar swiftui-navigationlink

我有以下内容:

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())
}

对于导航栏的配置,我有:

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)
        }
    }
}

但是由于某些原因,顶部状态栏被忽略了,并且颜色没有被应用到它:

Status bar image

如何将红色也应用于导航栏上方的状态栏,我希望它是相同的颜色。

我尝试了以下方法,但这失败了:

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

2 个答案:

答案 0 :(得分:2)

尝试以下操作(至少在Xcode 11.4 / iOS 13.4上进行了测试,它应该可以工作,但是有人报告说不是,所以可能是依赖的)

demo

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

       UINavigationBar.appearance().standardAppearance = navBarAppearance
}

答案 1 :(得分:0)

尝试使用此修饰符,因为这样可以让您自由地根据每个视图更改颜色。

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()
            }
        }
    }
}}

我在下面的视图中使用了它

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))
}}
相关问题