ios - 在 iOS 11 中忽略设置 UINavigationBar 外观

标签 ios swift uinavigationcontroller uinavigationbar

我正在尝试设置屏幕之间的 UINavigationBar 外观(tintColorbarTintColor 等),但目前在 iOS 11 中,大部分似乎都被完全忽略了或者没有按预期运行。当推送或弹出 View 时,单个导航 Controller 内的栏外观会发生变化。我有两个函数,我在 viewWillAppear 中调用它们。

我需要能够设置标题颜色、左右栏按钮项目颜色、后退按钮颜色和栏色调颜色。

我现在只想让颜色正常工作,所以我尝试了这个,但没有成功。

public func setDarkHeaderStyle() {
    UIApplication.shared.statusBarStyle = .lightContent

    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().barTintColor = Colours.secondaryNavy
    UINavigationBar.appearance().isTranslucent = false
}

public func setLightHeaderStyle() {
    UIApplication.shared.statusBarStyle = .default

    UINavigationBar.appearance().tintColor = Colours.primaryNavy
    UINavigationBar.appearance().barTintColor = UIColor.white
    UINavigationBar.appearance().isTranslucent = false
}

Dark style Light style

如果我改用导航 Controller 来设置颜色,它确实适用于条形色调、UIBarButtonItem 和后退按钮,但标题不正确。

public func setDarkHeaderStyle() {
    UIApplication.shared.statusBarStyle = .lightContent

    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.barTintColor = Colours.secondaryNavy
    navigationController?.navigationBar.isTranslucent = false
}

public func setLightHeaderStyle() {
    UIApplication.shared.statusBarStyle = .default

    navigationController?.navigationBar.tintColor = Colours.primaryNavy
    navigationController?.navigationBar.barTintColor = UIColor.white
    navigationController?.navigationBar.isTranslucent = false
}

Dark style Light style

所以我手动设置标题文本属性如下:

public func setDarkHeaderStyle() {
    UIApplication.shared.statusBarStyle = .lightContent

    navigationController?.navigationBar.titleTextAttributes = [
        NSAttributedStringKey.font: UIFont(name: Fonts.fontRegularName, size: 16)!,
        NSAttributedStringKey.kern: 0.2,
        NSAttributedStringKey.foregroundColor: UIColor.white
    ]

    navigationController?.navigationBar.tintColor = UIColor.white
    navigationController?.navigationBar.barTintColor = Colours.secondaryNavy
    navigationController?.navigationBar.isTranslucent = false
}

public func setLightHeaderStyle() {
    UIApplication.shared.statusBarStyle = .default

    navigationController?.navigationBar.titleTextAttributes = [
        NSAttributedStringKey.font: UIFont(name: Fonts.fontRegularName, size: 16)!,
        NSAttributedStringKey.kern: 0.2,
        NSAttributedStringKey.foregroundColor: Colours.primaryNavy
    ]

    navigationController?.navigationBar.tintColor = Colours.primaryNavy
    navigationController?.navigationBar.barTintColor = UIColor.white
    navigationController?.navigationBar.isTranslucent = false
}

这似乎可行,除了当您弹出回到 Root View 时未设置标题颜色:

Dark initial load Light initial load Dark popped

我想我有两个问题:

为什么 UINavigationBar.appearance() 不起作用? 我怎样才能让它可靠地工作?

最佳答案

我认为这是一个错误。 UIBarNavigationItem 出于某种原因似乎忽略了您对其标题属性和色调颜色的更改,除非您的标题文本发生更改。这是一种奇怪的行为,您可以考虑报告它。一种解决方法是将空白后缀切换到您的标题:

// Hack!!! adds and removes an empty space to the title to 
// force the bar item reset title attributes.
let title: String = barItem.title ?? ""
barItem.title = title.hasSuffix(" ") ? String(title.dropLast()) : title + " "

关于ios - 在 iOS 11 中忽略设置 UINavigationBar 外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48991028/

相关文章:

ios - Paging Swift 上的 UICollectionView 大小调整问题

ios - 在 Swift 中从选择器调用协议(protocol)函数

ios - 如何在主视图上添加详细 View ?

ios - 奇怪的NSURLConnection缓存行为

swift - 如何让 task2 在 task1 快速完成后执行?使用 dispatch_sync?

json - 使用 Json for Swift 3 的 zoho books 的 POST 方法

ios - 如何检测 UIPickerView 的变化?

ios - 如何向嵌入 UINavigationController 的 UICollectionViewController 添加菜单栏?

swift - 无法将 tabBarController.viewController 转换为 UINavigationController

iOS 7 : different navigation items for tab bar controller