Xcode 11.4。导航的标题颜色从故事板中变为黑色

Tud*_*opa 57 xcode navigationbar ios swift

我最近将我的 Xcode 更新到 11.4。当我在设备上运行该应用程序时,我注意到从故事板设置时,我的所有导航项的标题都变黑了。 在此处输入图片说明

您不能从代码中更改两者,以下代码行不再起作用

self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
Run Code Online (Sandbox Code Playgroud)

我只使用一些 iOS 13 的东西 UINavigationBarAppearance 让它工作

@available(iOS 13.0, *)
    private func setupNavigationBar() {
        let app = UINavigationBarAppearance()
        app.titleTextAttributes = [.foregroundColor: UIColor.white]
        app.backgroundColor = Constants.Color.barColor
        self.navigationController?.navigationBar.compactAppearance = app
        self.navigationController?.navigationBar.standardAppearance = app
        self.navigationController?.navigationBar.scrollEdgeAppearance = app

        self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
    }
Run Code Online (Sandbox Code Playgroud)

有人可以解释我为什么吗???这是一个关键的错误,还是一些新的隐藏功能?

Stu*_*ney 38

这为我修复了它,使用 UINavigationBarAppearance 代替,来自:Customizing Your App's Navigation Bar

if #available(iOS 13.0, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = UIColor.black
    appearance.titleTextAttributes = [.foregroundColor: UIColor.white] // With a red background, make the title more readable.
    self.navigationBar.standardAppearance = appearance
    self.navigationBar.scrollEdgeAppearance = appearance
    self.navigationBar.compactAppearance = appearance // For iPhone small navigation bar in landscape.
} else {
    self.navigationBar.barTintColor = UIColor.black
    self.navigationBar.tintColor = UIColor.white
    self.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
Run Code Online (Sandbox Code Playgroud)

注意:我继承了 UINavigationController,这是从viewWillAppear的覆盖中调用的。

...或对于AppDelegate,应用程序范围:

if #available(iOS 13.0, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = UIColor.black
    appearance.titleTextAttributes = [
        NSAttributedStringKey.foregroundColor: UIColor.white
    ]

    let buttonAppearance = UIBarButtonItemAppearance()
    buttonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.white]
    appearance.buttonAppearance = buttonAppearance

    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    UINavigationBar.appearance().compactAppearance = appearance

    UIBarButtonItem.appearance().tintColor = UIColor.white
} else {
    UINavigationBar.appearance().barTintColor = UIColor.black
    UINavigationBar.appearance().titleTextAttributes = [
        NSAttributedStringKey.foregroundColor: UIColor.white
    ]
    UINavigationBar.appearance().tintColor = UIColor.white

    UIBarButtonItem.appearance().tintColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)

...对于 AppDelegate,应用程序范围内,在 Objective-C 中:

if (@available(iOS 13, *)) {
    UINavigationBarAppearance *appearance = [[UINavigationBarAppearance alloc] init];
    [appearance configureWithOpaqueBackground];
    appearance.backgroundColor = UIColor.whiteColor;
    appearance.titleTextAttributes = titleAttributes;

    UIBarButtonItemAppearance *buttonAppearance = [[UIBarButtonItemAppearance alloc] init];
    buttonAppearance.normal.titleTextAttributes = barButtonItemAttributes;
    appearance.buttonAppearance = buttonAppearance;

    UINavigationBar.appearance.standardAppearance = appearance;
    UINavigationBar.appearance.scrollEdgeAppearance = appearance;
    UINavigationBar.appearance.compactAppearance = appearance;

    [[UINavigationBar appearance] setTintColor:UIColor.blackColor];
} else {
    [[UINavigationBar appearance] setBarTintColor:UIColor.whiteColor];
    [[UINavigationBar appearance] setTintColor:UIColor.blackColor];
    [[UINavigationBar appearance] setTranslucent:false];
    [[UINavigationBar appearance] setTitleTextAttributes: titleAttributes];
    [[UIBarButtonItem appearance] setTitleTextAttributes:barButtonItemAttributes forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)


小智 14

在情节提要上,对于您的导航控制器,将“Bar Tint”更改为其“默认”值,然后在您的代码中,您可以像往常一样更改它。

  • 最佳答案。真的。 (3认同)
  • 这才是正确的方法 (2认同)
  • @JCutting8 是的,没错。但在 Xcode 11.4 中,如果您没有在情节提要中设置默认颜色,则以编程方式更改它是行不通的。我不知道这是否是一个问题。 (2认同)

jam*_*ous 6

不知道是不是bug。

我们修复它的方法是在项目设置中将“状态栏样式”设置为深色或浅色内容。这将以某种方式强制状态栏文本颜色,而不是根据设备处于浅色或深色模式来确定。

此外,您需要在 Info.plist 中将值“基于控制器的状态栏外观”设置为“否”。如果没有该值,“状态栏样式”将被覆盖。

接下来创建一个自定义导航控制器并在您的故事板中实现它。

class CustomNavigationController: UINavigationController {

 override func viewDidLoad() {
    super.viewDidLoad()
    setNavBar()
 }

 func setNavBar() {
    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithOpaqueBackground()
        appearance.backgroundColor = UIColor.blue
        appearance.titleTextAttributes = [.foregroundColor: UIColor.yellow]
        self.navigationBar.standardAppearance = appearance
        self.navigationBar.scrollEdgeAppearance = appearance
        self.navigationBar.compactAppearance = appearance
    } else {
        self.navigationBar.barTintColor = UIColor.blue
        self.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.yellow]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

*颜色已设置,因此您可以清楚地看到它们的工作情况。

我发现在 ViewDidLoad 中设置代码比在 ViewDidAppear 中设置更好,因为我的颜色没有在初始加载时设置,只有在导航返回并重新加载之后。

我还发现这个问题可能与 NavBar 的“Bar Tint”有关。当我们第一次尝试解决它时,我们将“Bar Tint”设置为默认值,这似乎也解决了错误。然而,它做到了,所以我们无法获得我们想要的 NavBar 背景颜色。所以在我的故事板中,我确保将此值设置为默认值,只是为了更好地衡量。

希望能帮助到你