Changing Navigation Bar Color in Swift

Changing navigation bar color in Swift

Navigation Bar:

navigationController?.navigationBar.barTintColor = UIColor.green

Replace greenColor with whatever UIColor you want, you can use an RGB too if you prefer.

Navigation Bar Text:

navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]

Replace orangeColor with whatever color you like.

Tab Bar:

tabBarController?.tabBar.barTintColor = UIColor.brown

Tab Bar Text:

tabBarController?.tabBar.tintColor = UIColor.yellow

On the last two, replace brownColor and yellowColor with the color of your choice.

swift: change the color of the navigation bar

Instead of barTintColor, use backgroundColor to change the navigationBar'r color, i.e.

In FirstVC,

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

self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.backgroundColor = .red //here...
}
}

In SecondVC,

class SecondVC: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.backgroundColor = .clear //here...
}
}

Sample Image

Changing Navigation Bar Color in Xcode

You just need to click on the navbar and in attribute inspector, there is an option for Background, you can provide any color which you want to give. In my case, I selected a custom sky blue color.

Check Screenshot here

How to set navigation bar background color across all scenes

You have many ways to do that:
First
Create a superViewController and all other view controllers inherit this one:

import UIKit

class SuperViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.navigationBar.barTintColor = UIColor.orange
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Other view controllers like that:

class ViewController: SuperViewController {

@IBOutlet weak var pageVC: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Now you don,t need to set navigation color in application only use Inherit superViewcontroller in every view controller. You can add all common methods which you are going to follow in every controller.

Second way to do that

///Common NavigationController for every controller used in application
class NavigationController: UINavigationController {

// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.navigationBar.barTintColor = UIColor.red
self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Use this navigation controller in everywhere in you app. When you need
navigation controller then give this class in the navigation controller.

Swift & Navigation : Navigation Bar changes its background color when scroll the view

Paste this to AppDelegate and if you have tab bar also then paste tabbarappearance also it will work.

if #available(iOS 15, *) {
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.foregroundColor : UIColor.white
]
navigationBarAppearance.backgroundColor = UIColor.blue
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance

let tabBarApperance = UITabBarAppearance()
tabBarApperance.configureWithOpaqueBackground()
tabBarApperance.backgroundColor = UIColor.blue
UITabBar.appearance().scrollEdgeAppearance = tabBarApperance
UITabBar.appearance().standardAppearance = tabBarApperance
}

Change UINavigationbar background colour and title font/colour programmatically

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.

How to dynamically change navigation bar color on ios15?

In your VC you can try adjusting the view.backgroundColor

      UINavigationBar?.view.backgroundColor = MY_COLOR

Change the color of iOS Navigation Bar

In fact, i found that the solution was to use in the AppDelegate.siwft :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
UINavigationBar.appearance().barTintColor = UIColor(red: 0, green: 0/255, blue: 205/255, alpha: 1)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]

return true
}

and then in each view controller, that we need another background color or something else

  1. the segue should be different than "show"

  2. use the func viewWillAppear

     override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
    self.navigationController?.navigationBar.tintColor = UIColor.blueColor()
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()]
    }


Related Topics



Leave a reply



Submit