2

我正在更新一个在 Xcode 10 上编译并在 iOS 13 上运行良好的应用程序。我想做一些更改,所以在 Xcode 11 上重新编译,现在 barTintColor 出现问题。

如果“Large Titles”设置为“Always”,我的自定义 barTintColor 不会被应用——我只是得到默认的灰色。如果“大标题”设置为“从不”,我的自定义 barTintColor 将按预期应用。如果“大标题”设置为“自动”,则显示大标题时导航栏默认为灰色,显示小标题时为我的自定义颜色。比如我的nav bar下面的TableView被推上去的时候,默认的大标题切换到小标题,我的NavBar变色。正常行为是它始终是我的自定义颜色。

我的 ViewController 类中的相关代码,最后一行是设置 barTintColor 的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    setDelegates()
    setTableViewHeightForCollapsingHeaders()
    setNavigtionBarItems()
    doSplitViewManagement()
}


override func viewWillAppear(_ animated: Bool) {
    clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
    super.viewWillAppear(animated)
    updateUI()
}

fileprivate func setNavigtionBarItems() {
    //set up UI buttons
    navigationItem.leftBarButtonItem = editButtonItem
    let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(insertNewObject(_:)))
    navigationItem.rightBarButtonItem = addButton

    navigationController?.navigationBar.barTintColor = UIColor(hex: 0x5da0a2)

}

知道为什么行为会改变,以及如何解决吗?

4

3 回答 3

7

glotcha 指出的苹果文档对于解决问题至关重要,尽管还有更多内容。这是适用于 iOS 13 的更新版本setNavigationBarItems()

fileprivate func setNavigtionBarItems() {

    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithDefaultBackground()
        appearance.backgroundColor = myBackgroundColor

        navigationController?.navigationBar.standardAppearance = appearance
        navigationController?.navigationBar.scrollEdgeAppearance = appearance
        //navigationController?.navigationBar.compactAppearance = appearance
        
    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = myBackgroundColor
    }
}

在我的案例中,一个关键点是我的导航栏(在自动布局中)设置为“大标题”为“自动”。这使得有必要包含该.scrollEdgeAppearance行,以便在从大到紧凑转换时应用自定义外观。事实证明.compactAppearance不需要这条线,因为我对两者都使用了相同的颜色。如果我想要大型和紧凑的不同外观设置,那么一条线.compactAppearance也很有用。

于 2020-03-22T20:58:06.090 回答
2

自 iOS 13 以来有一个新的 API https://developer.apple.com/documentation/uikit/uinavigationbarappearance

您正在寻找的 backgroundColor 属性在超类中 https://developer.apple.com/documentation/uikit/uibarappearance

这里有一些额外的示例代码 https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar

于 2020-03-22T02:05:11.020 回答
1

如果您想要一个地方为整个应用程序执行此操作:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let barBackgroundColor = UIColor(displayP3Red: 47/255, green: 54/255, blue: 64/255, alpha: 1.0)
    
    let appearance = UINavigationBarAppearance()
    appearance.configureWithDefaultBackground()
    appearance.backgroundColor = barBackgroundColor
    appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

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

    return true
}
于 2021-07-17T15:05:19.880 回答