barTintColor not applied when NavigationBar is Large Titles

烈酒焚心 提交于 2021-02-07 09:56:54

问题


I'm updating an app that was compiled on Xcode 10 and running fine up through iOS 13. I wanted to make some changes so recompiled on Xcode 11 and now have a problem with the barTintColor.

If 'Large Titles' is set to 'Always' my custom barTintColor is not applied -- I just get the default gray. If 'Large Titles' is set to 'Never', my custom barTintColor is applied as expected. If 'Large Titles' is set to 'Automatic', the NavBar is default gray when Large Titles are shown and my custom color when small titles are shown. For example, when the TableView below my nav bar is pushed up, the default large title switches to small title, and my NavBar changes color. Normal behavior would be for it always to be my custom color.

The relevant code from my ViewController class, with the last line being the one that sets the 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)

}

Any idea why the behavior changed, and how to fix it?


回答1:


There's a new API since iOS 13 https://developer.apple.com/documentation/uikit/uinavigationbarappearance

The backgroundColor property you're looking for is in the super class https://developer.apple.com/documentation/uikit/uibarappearance

Some extra sample code here https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar




回答2:


Apple's documentation that glotcha pointed to was critical to solving the problem although there was a bit more to it. Here's the updated version of my setNavigationBarItems() that works in iOS 13:

    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
    }

A key point in my case was that my Navigation Bar is set (in Autolayout) with 'Large Titles' as 'Automatic'. This makes it necessary to include the .scrollEdgeAppearance line so that the custom appearance applies when it transitions from large to compact. It turned out the .compactAppearance line was not required because I use the same color for both. If I wanted different appearance settings for large and compact then a line with .compactAppearance would also be useful.



来源:https://stackoverflow.com/questions/60795035/bartintcolor-not-applied-when-navigationbar-is-large-titles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!