当我的基于SwiftUI的应用在横向模式下启动时,导航栏上的背景色未设置 [英] Background color on Navigation Bar is not set when my SwiftUI based app is launched in landscape mode

查看:75
本文介绍了当我的基于SwiftUI的应用在横向模式下启动时,导航栏上的背景色未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用基于 SwiftUI 的应用程序,该应用程序依赖 NavigationView 从屏幕过渡.

I am working with a SwiftUI based app that relies on a NavigationView to transition from screens.

我需要在导航栏上设置背景颜色,并且找到了使该功能在大多数情况下都能正常工作的代码.

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.

我还注销了 NavigationConfigurator ,发现以肖像模式启动时,有两个调用.第一个找不到导航控制器,但是第二个找到.

I have also logged out the NavigationConfigurator and have found that when it launches in portrait mode, there are two calls made. The first one fails to find the navigation controller, but the second one does.

当我以横向模式启动时,只会进行一次找不到的呼叫.旋转后,它会找到并成功更新颜色.

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.

我确实尝试通过 @State 管理强制重绘,但这没有成功.

I did attempt to force redraws via @State management, but that was unsuccessful.

将应用程序锁定为纵向模式的时间很短,我的想法已经用完了.

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

struct ContentView: View {
    init() {
        UINavigationBar.appearance().barTintColor = UIColor.orange
    }
    // .. your other code here

此解决方案在任何初始方向都可以正常工作.经过Xcode 11.4的测试.

This solution works fine with any initial orientation. Tested with Xcode 11.4.

备用:

如果您仍然希望通过配置器进行访问,那么根据我的经验,以下解决方案(经过测试并有效)会更可靠

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

这篇关于当我的基于SwiftUI的应用在横向模式下启动时,导航栏上的背景色未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆