Search code examples
iosswiftxcodeuinavigationcontroller

Set border and color of navigationbar when largetitles = true


I set my navigation bar with preferslargetitles = true. For some reason, the color and the bottom border disappear and it's white. How can I keep the color and the border of this like if it's standard?

This is how it looks like with large titles:

enter image description here

I would like to have the same color and border as in this:

enter image description here

I already tried to set the background color, but the border is still missing and the status bar is in a different color.

navigationController?.navigationBar.backgroundColor = UIColor(displayP3Red: 248/255, green: 248/255, blue: 248/255, alpha: 1)

Solution

  • I got this to work now. If anyone has the same problem it can be done with this code:

    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
            appearance.backgroundColor = UIColor(displayP3Red: 248/255, green: 248/255, blue: 248/255, alpha: 1)
            appearance.titleTextAttributes = [.foregroundColor: UIColor.black]
            appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
    
            UINavigationBar.appearance().tintColor = .systemBlue
            UINavigationBar.appearance().standardAppearance = appearance
            UINavigationBar.appearance().compactAppearance = appearance
            UINavigationBar.appearance().scrollEdgeAppearance = appearance
    } else {
        UINavigationBar.appearance().tintColor = .systemBlue
        UINavigationBar.appearance().barTintColor = UIColor(displayP3Red: 248/255, green: 248/255, blue: 248/255, alpha: 1)
        UINavigationBar.appearance().isTranslucent = false
    }
    

    You need to put this in

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { }

    otherwise, the first view controller does not have these changes, because the first view is already loaded.

    Thanks to @Sebastion who post something similar in this post: iOS13 Navigation bar large titles not covering status bar