Mhy my NavigationBar Colour is not changing in Swift?

64 views Asked by At

I made a custom NavigationBar to use on all screens like below:

 import Foundation
 import UIKit

extension UIViewController {

func setUpNavigationBar() {

    self.navigationController?.navigationBar.backgroundColor = .yellow
    self.navigationController?.navigationBar.isTranslucent = false
    self.navigationController?.navigationBar.layer.masksToBounds = false
    
    let menuImageView = UIImageView.init(image: #imageLiteral(resourceName: "menu8"))
    menuImageView.tintColor = .darkGray
    menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
    menuImageView.contentMode = .scaleAspectFit
    menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
    let btnMenu = UIBarButtonItem(customView: menuImageView)
    
    self.navigationItem.setLeftBarButton(btnMenu, animated: true)
}

@objc func menuClick(){
    SideMenuController.preferences.basic.menuWidth = self.view.frame.width*0.8
    if !(self.sideMenuController?.isMenuRevealed ?? false){
        self.sideMenuController?.revealMenu()
    }
  }    
}

and calling in SignupVC ViewWillAppear like this:

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

With the above code, I am not getting a yellow colour NavigationBar. I am getting NavigationBar in SignupVC like the only white colour. No leftbutton with the menu. Why? Where am I wrong?

Thank you.

1

There are 1 answers

0
hoseinali alborzi On BEST ANSWER

For change background :

navigationController?.navigationBar.barTintColor = UIColor.yellow

Tested ok 100%

this code :

func setUpNavigationBar() {

    navigationController?.navigationBar.barTintColor = UIColor.yellow
    self.navigationController?.navigationBar.isTranslucent = false
    self.navigationController?.navigationBar.layer.masksToBounds = false
    
    let menuImageView = UIImageView.init(image: UIImage.init(named: "day")!)
    menuImageView.tintColor = .darkGray
    menuImageView.frame = CGRect(x:0.0,y:0.0, width:25,height:25.0)
    menuImageView.contentMode = .scaleAspectFit
    menuImageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.heightAnchor.constraint(equalToConstant: 25).isActive = true
    menuImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(menuClick)))
    let btnMenu = UIBarButtonItem(customView: menuImageView)
    
    self.navigationItem.setLeftBarButton(btnMenu, animated: true)
}