1
\$\begingroup\$

The design of my application consists of multiple viewControllers which all have different styling applied to them. The following code is used in every viewController to style the viewController:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.title = "PROFILE"

        self.navigationController?.view.backgroundColor = .white
        self.navigationController?.navigationBar.tintColor = self.view.tintColor

        navigationController?.navigationBar.barStyle = .default

        tabBarController?.tabBar.isTranslucent = false
        tabBarController?.tabBar.backgroundColor = .white
        tabBarController?.tabBar.tintColor = .black
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        self.navigationController?.navigationBar.tintColor = .white
        self.navigationController?.view.backgroundColor = UIColor.clear
        self.navigationController?.navigationBar.barStyle = .blackTranslucent

        self.navigationController?.navigationBar.shadowImage = UIImage()
    }

Some properties change per VC, for example the backgroundColor of the VC. This code is copy pasted in every VC I make with small changes, which causes a lot of duplicate code etc. Is there a design pattern which allows me to create the styling of every VC more conveniently?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

extension UIViewController create a navigation bar Method call anywhere from ViewController in your project.navigation bar will appear and add more parameter color or other relevant change you want to achieved .

for example

    import UIKit

    extension UIViewController {

        func setupNavigationBar(title: String) {
            // back button without title
            //self.navigationController?.navigationBar.topItem?.title = ""

            //back button color
            //self.navigationController?.navigationBar.tintColor = UIColor.white

            //set titile
             self.navigationItem.title =  title

            //set text color & font size
            //self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.init(red: 251/255, green: 251/255, blue: 251/255, alpha: 1) , NSFontAttributeName:UIFont.systemFont(ofSize: 19)]

            //set background color without gradian effect
            //self.navigationController?.navigationBar.barTintColor = UIColor.init(red: 134/255, green: 145/255, blue: 152/255, alpha: 1)

            //show right button 
            let rightButton = UIBarButtonItem(image: #imageLiteral(resourceName: "Menu"), style: .plain, target: self, action: #selector(menu))

            //right Bar Button Item tint color 
            //self.navigationItem.rightBarButtonItem?.tintColor = UIColor.init(red: 219/255, green: 219/255, blue: 219/255, alpha: 1)

            //show the Menu button item
            self.navigationItem.rightBarButtonItem = rightButton

            //show bar button item tint color 
            //self.navigationItem.rightBarButtonItem?.tintColor = UIColor.init(red: 219/255, green: 219/255, blue: 219/255, alpha: 1)

        }

        func menu(){ 
            print("showSlideOutMane fire ")
        }

    }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.