Search code examples
iosswiftswift4ios11appdelegate

Change UINavigationbar background colour and title font/colour programmatically


I want to change the navigation bar background colour, title font and colour programmatically in iOS 11 and swift 4 from AppDelegate. I know how to do it using Xcode but didn't find up-to-date solution for doing it programmatically.


Solution

  • You can use the following code to change background colour and Title font.

    func setupNavigationBarAppearance() {
        UINavigationBar.appearance().tintColor = .black
        UINavigationBar.appearance().shadowImage = UIImage.imageFromColor(.black, width: 1.0, height: 1.0)?.resizableImage(withCapInsets: .zero, resizingMode: .tile)
        UINavigationBar.appearance().isTranslucent = false
    
        let font:UIFont = UIFont(name: "ProximaNova-Bold", size: 18.0)!
        let navbarTitleAtt = [
            NSAttributedStringKey.font:font,
            NSAttributedStringKey.foregroundColor: UIColor.white
        ]
        UINavigationBar.appearance().titleTextAttributes = navbarTitleAtt
    }
    

    And call this func in didFinishLaunchingWithOptions as setupNavigationBarAppearance(). I am using this same code, and it is working fine.