iOS Swift Customize Navigation Bar example

You can do this in the AppDelegate and it will be same for all the views pushed using the Navigation Controller

func application(_ application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let mainStoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let mainViewController = mainStoryboard.instantiateInitialViewController()
        let navigationController = UINavigationController(rootViewController: mainViewController!)
        
        //Customize Navigation Bar
        let navigationBarAppearance = navigationController.navigationBar
        navigationBarAppearance.tintColor = UIColor.black
        navigationBarAppearance.barTintColor = UIColor.lightGray
        let attributes = [
            NSAttributedString.Key.foregroundColor: UIColor.black,
            NSAttributedString.Key.font: UIFont(name: "Georgia-Bold", size: 24)!
        ]
        navigationBarAppearance.titleTextAttributes = attributes
        navigationBarAppearance.isTranslucent = false
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()
        
        return true
    }
iOS Swift Customize Navigation Bar
iOS Swift Customize Navigation Bar

Another way to do this is create an extension of UINavigationController.
Source: UINavigationController+CustomTheme.swift


import UIKit

enum NavigationBarMode {
    case normal
    case alternate
}

extension UINavigationController {
    
    func themeNavigationBar(mode: NavigationBarMode) {
        
        self.navigationBar.isTranslucent = false
        
        switch mode {
       
        case .normal:
            navigationBar.tintColor = UIColor.black
            navigationBar.barTintColor = UIColor.lightGray
            let attributes = [
                NSAttributedString.Key.foregroundColor: UIColor.black,
                NSAttributedString.Key.font: UIFont(name: "Georgia-Bold", size: 24)!
            ]
            navigationBar.titleTextAttributes = attributes
       
        case .alternate:
            navigationBar.tintColor = UIColor.lightGray
            navigationBar.barTintColor = UIColor.black
            let attributes = [
                NSAttributedString.Key.foregroundColor: UIColor.orange,
                NSAttributedString.Key.font: UIFont(name: "Georgia-Bold", size: 24)!
            ]
            navigationBar.titleTextAttributes = attributes
        }
    }

}

And then call the function when your View will appear and disappear in the View Controller


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.themeNavigationBar(mode: .alternate)
        
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.themeNavigationBar(mode: .normal)
    
    }

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.