Customize UINavigationBar Appearance Programmatically in UIViewController

The following Swift code example will demonstrate how we can customize UINavigationBar appearance in UIViewController. It will also cover how to:

  • Set UINavigationBar tint color,
  • Set Navigation bar Title color,
  • Set navigation bar ItemButton tint color,
  • Set Navigation bar background image.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
                // Set navigation bar background colour
        self.navigationController!.navigationBar.barTintColor = UIColor.yellow
        
        // Set navigation bar title text colour
        self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
        
        // Set Navigation bar background Image
        let navBgImage:UIImage = UIImage(named: "bg_blog_navbar_reduced.jpg")!
        self.navigationController!.navigationBar.setBackgroundImage(navBgImage, for: .default)
    }
 
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published.