简体   繁体   English

当我的基于 SwiftUI 的应用程序以横向模式启动时,导航栏上的背景颜色未设置

[英]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.

相关问题 SwiftUI - 设置状态栏背景颜色以与导航栏对齐 - SwiftUI - Set statusbar background color to align with navigation bar 带有 ScrollView 和导航栏 SwiftUI 的屏幕背景颜色 - Screen Background Color With ScrollView And Navigation Bar SwiftUI 有没有办法在应用程序中普遍设置背景颜色或导航栏? - Is there a way to set the background color or navigation bar universally across an app? CNContactViewController导航栏背景颜色与我的应用不匹配 - CNContactViewController navigation bar background color is not matching with my app 使用 SwiftUI 将设备旋转到横向时出现导航栏 - Navigation Bar appearing when device is rotated to landscape using SwiftUI 导航栏的横向模式问题 - landscape mode problem with a navigation bar 如何更改 SwiftUI 中导航栏的背景颜色? - How can I change the background color of the navigation bar in SwiftUI? 如何在 SwiftUI 中设置清晰/透明背景的导航栏? - How to set a navigation bar in clear / transparent background in SwiftUI? 导航栏在横向模式中向上移动 - iOS - Navigation bar shifts up in the landscape mode - iOS SwiftUI 在 TabView 内导航和状态栏的颜色/透明度发生冲突 - SwiftUI Navigation and status bar clash in color/transparency when inside TabView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM