SwiftUI - 导航栏颜色更改未应用于状态栏 [英] SwiftUI - Navigation bar colour change not being applied to status bar
本文介绍了SwiftUI - 导航栏颜色更改未应用于状态栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下几点:
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
}
推荐答案
尝试以下操作(它应该可以工作,至少在 Xcode 11.4/iOS 13.4 上测试过,但有人报告它不是,所以它可能是依赖的)
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
}
这篇关于SwiftUI - 导航栏颜色更改未应用于状态栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文