简体   繁体   English

Xcode 11.4。 导航的标题颜色从 storyboard 变为黑色

[英]Xcode 11.4. Navigation's Title Color gone BLACK from storyboard

I recently updated my Xcode to 11.4.我最近将我的 Xcode 更新到 11.4。 When I run the app on the device, i've noticed that all my navigations item's titles gone fully black when being set from storyboard.当我在设备上运行该应用程序时,我注意到从 storyboard 设置时,我的所有导航项的标题都变成了全黑。 在此处输入图像描述

You can't change neither from code, the following line of code doesn't work anymore您不能从代码中进行更改,以下代码行不再起作用

self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]

I only make it work using some iOS 13 stuffs UINavigationBarAppearance我只使用一些 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]
    }

Can somebody explain me why???谁能解释一下为什么??? This is a crucial bug, or some new hidden feature?这是一个关键的错误,还是一些新的隐藏功能?

This fixed it for me, using UINavigationBarAppearance instead, from: Customizing Your App's Navigation Bar这为我修复了它,使用 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]
}

Note: I subclassed UINavigationController , and this was called from the override of viewWillAppear .注意:我继承了 UINavigationController ,这是从viewWillAppear的覆盖中调用的。

...or for AppDelegate , app-wide: ...或对于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
}

...for AppDelegate, app-wide, in Objective-C: ...对于 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];
}

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

Not sure if it's a bug or not.不知道是不是bug。

The way we fixed it is by setting the "Status Bar Style" to either dark or light content in project setting.我们修复它的方法是在项目设置中将“状态栏样式”设置为深色或浅色内容。 This will force the Status Bar text color a certain way rather than being determined based on the devices being in Light or Dark mode.这将以某种方式强制状态栏文本颜色,而不是根据设备处于浅色或深色模式来确定。

In addition, you need to set the value "View controller-based status bar appearance" to "NO" in your Info.plist.此外,您需要在 Info.plist 中将值“基于控制器的状态栏外观”设置为“否”。 without that value the "Status Bar style" will be overridden.如果没有该值,“状态栏样式”将被覆盖。

Next create a custom navigation controller and implement it in your storyboards.接下来创建一个自定义导航控制器并在您的故事板中实现它。

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]
    }
  }
}

*Colors are set so you can see them clearly working. *颜色已设置,因此您可以清楚地看到它们的工作情况。

I found it was better to set the code in ViewDidLoad rather than ViewDidAppear because my colors were not being set on the initial load, only after navigating back and reloading.我发现在 ViewDidLoad 中设置代码比在 ViewDidAppear 中设置更好,因为我的颜色没有在初始加载时设置,只有在导航返回并重新加载之后。

I also found that this issue might be tied to the "Bar Tint" of a NavBar.我还发现这个问题可能与 NavBar 的“Bar Tint”有关。 when we were first trying to resolve it, we set the "Bar Tint" to default and that seemed resolve the error too.当我们第一次尝试解决它时,我们将“Bar Tint”设置为默认值,这似乎也解决了错误。 However, it made it so we couldn't get the NavBar background color what we wanted.然而,它做到了,所以我们无法获得我们想要的 NavBar 背景颜色。 So in my storyboards I made sure to set this value to default just for good measure.所以在我的故事板中,我确保将此值设置为默认值,只是为了更好地衡量。

Hope it helps希望能帮助到你

no need for the workaround.it is a bug in Xcode Interface Builder.不需要解决方法。这是 Xcode Interface Builder 中的一个错误。 Apple release Update for Xcode 11.4.1 Apple 发布 Xcode 11.4.1 更新

from Apple developer release notes来自 Apple 开发者发布说明

Interface Builder界面生成器

Fixed an issue that caused some UINavigationBar appearance properties set in storyboard and XIB documents to be ignored when building with Xcode 11.4.修复了在使用 Xcode 11.4 构建时导致在 storyboard 和 XIB 文档中设置的某些 UINavigationBar 外观属性被忽略的问题。 (60883063) (FB7639654) (60883063) (FB7639654)

https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_1_release_notes https://developer.apple.com/documentation/xcode_release_notes/xcode_11_4_1_release_notes

Similar to Stu Carney's response on 3/25, I added a few more implementation details.与 Stu Carney 3/25 的回复类似,我添加了更多实现细节。

Create a subclass of UINavigationController .创建UINavigationController的子类。 Add the following to viewWillAppear:将以下内容添加到 viewWillAppear:

let isDarkMode = UserDefaults.standard.bool(forKey: "DarkMode")
let titleColor: UIColor = isDarkMode ? .white : .black
let navBarColor: UIColor = isDarkMode ? .black : .white
let tintColor: UIColor = isDarkMode ? .yellow : .red  //back button text and arrow color, as well as right bar button item

if #available(iOS 13.0, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    appearance.backgroundColor = navBarColor
    appearance.titleTextAttributes = [.foregroundColor: titleColor]
    appearance.largeTitleTextAttributes = [.foregroundColor: titleColor]

    self.navigationBar.standardAppearance = appearance
    self.navigationBar.scrollEdgeAppearance = appearance
    self.navigationBar.compactAppearance = appearance // For iPhone small navigation bar in landscape.

    self.navigationBar.tintColor = tintColor //changes back button text and arrow color, as well as right bar button item
} else {
    self.navigationBar.barTintColor = navBarColor
    self.navigationBar.tintColor = tintColor
    self.navigationBar.titleTextAttributes = [.foregroundColor: titleColor]
    self.navigationBar.largeTitleTextAttributes = [.foregroundColor: titleColor]
}

Then override preferredStatusBarStyle :然后覆盖preferredStatusBarStyle

override var preferredStatusBarStyle: UIStatusBarStyle {
    let isDarkMode = UserDefaults.standard.bool(forKey: "DarkMode")
    return isDarkMode ? .lightContent : .default
}

If you want to update the navigation bar and status bar dynamically, like from a UISwitch IBAction or selector method, add the following:如果要动态更新导航栏和状态栏,例如从 UISwitch IBAction 或选择器方法,请添加以下内容:

navigationController?.loadView()
navigationController?.topViewController?.setNeedsStatusBarAppearanceUpdate()

Also, be sure to set all your navigation bars and bar buttons to the default colors in IB.另外,请确保将所有导航栏和栏按钮设置为 IB 中的默认颜色。 Xcode seems to have a bug where the the IB colors override the colors set programatically. Xcode 似乎有一个错误,其中 IB 颜色覆盖了以编程方式设置的颜色。

In my case, after I upgraded Xcode from 11.3 to 11.4 this bug occurred.就我而言,在我将 Xcode 从 11.3 升级到 11.4 后,出现了此错误。 So I have to change my code to blow in order to set an image as background in the navigation bar.所以我必须更改我的代码以在导航栏中将图像设置为背景。

if #available(iOS 13.0, *) {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithOpaqueBackground()
    let backgroundImage = UIImage(named: "{NAVBAR_IMAGE_NAME}")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
    appearance.backgroundImage = backgroundImage
    self.navigationController?.navigationBar.compactAppearance = appearance
    self.navigationController?.navigationBar.standardAppearance = appearance
    self.navigationController?.navigationBar.scrollEdgeAppearance = appearance        
} else {
    self.navigationController?.navigationBar.barTintColor = Utils.themeColor
    let backgroundImage = UIImage(named: "{NAVBAR_IMAGE_NAME}")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch)
    self.navigationController?.navigationBar.setBackgroundImage(backgroundImage, for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

I had the same issue and was wondering that coding is not the solution.我有同样的问题,想知道编码不是解决方案。 There MUST be a way.必须有办法。 And found out that checking any option under Navigation bar -> Appearances in storyboard attribute inspector makes the title go black.并发现检查导航栏下的任何选项 - > storyboard 属性检查器中的外观会使标题 go 变黑。 So uncheck all of them.所以取消选中所有这些。 But not sure how I can have scroll Edge option on to color the status bar as well and still get the title colored.但不知道如何让滚动边缘选项也为状态栏着色并且仍然使标题着色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM