[英]Background color on Navigation Bar is not set when my SwiftUI based app is launched in landscape mode
I am working with a SwiftUI based app that relies on a NavigationView
to transition from screens.我正在使用基于SwiftUI的应用程序,该应用程序依赖于
NavigationView
从屏幕转换。
I have a requirement to set the background color on the navigation bar and have found code that makes this work most of the time.我需要在导航栏上设置背景颜色,并且找到了大部分时间都可以使用的代码。
When the app is launched in portrait mode, everything works properly across rotations.当应用程序以纵向模式启动时,一切都在旋转中正常工作。
However, when the app is launched in landscape mode, the bar is the default gray and only updates after the first rotation.但是,当应用程序以横向模式启动时,条形图默认为灰色,并且仅在第一次旋转后更新。
Below, I have the minimal amount of code to recreate my problem:下面,我有最少的代码来重新创建我的问题:
import SwiftUI
struct ContentView: View {
var body: some View {
return NavigationView {
List {
NavigationLink(destination: Text("A")) {
Text("See A")
}
}
.background(NavigationConfigurator { navigationConfigurator in
navigationConfigurator.navigationBar.barTintColor = .orange
})
.navigationBarTitle(Text(verbatim: "Home"), displayMode: .inline)
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct NavigationConfigurator: UIViewControllerRepresentable {
var configure: (UINavigationController) -> Void = { _ in }
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController {
UIViewController()
}
func updateUIViewController(_ uiViewController: UIViewController,
context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
if let navigationController = uiViewController.navigationController {
self.configure(navigationController)
print("Successfully obtained navigation controller")
} else {
print("Failed to obtain navigation controller")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
How the app appears when launched in portrait mode:以纵向模式启动时应用程序的显示方式:
...and rotated to landscape... ...并旋转到横向...
Finally, how it looks when launched in landscape mode.最后,以横向模式启动时的外观。
I have also logged out the NavigationConfigurator
and have found that when it launches in portrait mode, there are two calls made.我还注销了
NavigationConfigurator
,发现当它以纵向模式启动时,会进行两次调用。 The first one fails to find the navigation controller, but the second one does.第一个找不到导航 controller,但第二个找到了。
When I launch in landscape mode, only a single call is made which fails to find it.当我以横向模式启动时,只进行了一次调用,但未能找到它。 Upon rotation, it then finds it and successfully updates the color.
旋转后,它会找到它并成功更新颜色。
I did attempt to force redraws via @State
management, but that was unsuccessful.我确实尝试通过
@State
管理强制重绘,但没有成功。
Short of locking the app to portrait mode, I have run out of ideas.由于没有将应用程序锁定为纵向模式,我已经没有想法了。
If it is only about bar tint color and not needed navigation controller for more then it is simpler to use appearance, as如果它只是关于条形颜色而不需要导航 controller 更多那么使用外观更简单,因为
struct ContentView: View {
init() {
UINavigationBar.appearance().barTintColor = UIColor.orange
}
// .. your other code here
This solution works fine with any initial orientation.此解决方案适用于任何初始方向。 Tested with Xcode 11.4.
使用 Xcode 11.4 进行测试。
Alternate:备用:
If you still want access via configurator, the following solution (tested & worked) by my experience is more reliable如果您仍想通过配置器访问,根据我的经验,以下解决方案(经过测试和工作)更可靠
struct NavigationConfigurator: UIViewControllerRepresentable {
var configure: (UINavigationController) -> Void = { _ in }
func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController {
let controller = UIViewController()
DispatchQueue.main.async {
if let navigationController = controller.navigationController {
self.configure(navigationController)
print("Successfully obtained navigation controller")
} else {
print("Failed to obtain navigation controller")
}
}
return controller
}
func updateUIViewController(_ uiViewController: UIViewController,
context: UIViewControllerRepresentableContext<NavigationConfigurator>) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.