Search code examples
iosswiftuinavigationbaruinavigationbarappearance

barTintColor not applied when NavigationBar is Large Titles


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?


Solution

  • 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