How to Change Uinavigationbar Background Color from the Appdelegate

How to change UINavigationBar background color from the AppDelegate

You can use [[UINavigationBar appearance] setTintColor:myColor];

Since iOS 7 you need to set [[UINavigationBar appearance] setBarTintColor:myColor]; and also [[UINavigationBar appearance] setTranslucent:NO].

[[UINavigationBar appearance] setBarTintColor:myColor];
[[UINavigationBar appearance] setTranslucent:NO];

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.

Delegate function in appDelegate doesn't change navbar colors

I'd suggest, since you want to dynamically change the NavigationBar theme (like background color) based on a Flag model value, to not go for the AppDelegate way, as that will do it once for you, and it is thought more as a global way to set the NavigationBar style before any view is actually created.

There are a few ways you can apply that, like through extension ViewController, inheritance with base class.. and as well different ways you can get/set the flag values to change the navigation colors, like through userdefaults, variables... I'll show an example just to get you going:

import UIKit

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

currentFlag = "Canada"
}
}

class BaseViewController: UIViewController {

var currentFlag: String = "General" {
didSet {
setNavBarColor()
}
}

private let themeColorUS = UIColor(red: 0.991, green: 0.621, blue: 0.022, alpha: 1.00)
private let themeColorCanada = UIColor(red: 0.001, green: 0.686, blue: 0.000, alpha: 1.00)
private let themeColorGeneral = UIColor(red: 0.000, green: 0.954, blue: 0.969, alpha: 1.00)

override func viewDidLoad() {
super.viewDidLoad()

setNavBarColor()
}

private func setNavBarColor() {
navigationController?.navigationBar.barTintColor = getBarColor(for: currentFlag)
}

private func getBarColor(for flag: String) -> UIColor {
if flag == "US" {
return themeColorUS
} else if flag == "Canada" {
return themeColorCanada
}

return themeColorGeneral
}
}

And that means, we removed the global way of setting its style from AppDelegate so my didFinishLaunchingWithOptions looks like:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

return true
}

Running the following code (with flag set to Canada for my ViewController), and having ViewController in storyboard with a root viewcontroller of UINavigationController like this:

Would make the app look like:



Refactoring for improvements

Something you can do in addition, just to make it easier to manage the code and flags and colors, is to organise them in a structure, I choosed an enum as an example but you can do it other ways as well, just to give you a sample you could have done it this way:

import UIKit

enum Flag {
case us
case canada
case general

static let `default` = Flag.general

init(rawValue: String) {
switch rawValue {
case "US":
self = .us
case "Canada":
self = .canada
case "General":
self = .general
default:
self = .default
}
}

var themeColor: UIColor {
switch self {
case .us:
return UIColor(red: 0.001, green: 0.686, blue: 0.000, alpha: 1.00)
case .canada:
return UIColor(red: 0.001, green: 0.686, blue: 0.000, alpha: 1.00)
case .general:
return UIColor(red: 0.000, green: 0.954, blue: 0.969, alpha: 1.00)
}
}
}

class ViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()

currentFlag = .canada
}
}

class BaseViewController: UIViewController {

var currentFlag: Flag = .default {
didSet {
setNavBarColor()
}
}

override func viewDidLoad() {
super.viewDidLoad()

setNavBarColor()
}

private func setNavBarColor() {
navigationController?.navigationBar.barTintColor = currentFlag.themeColor
}
}

How to change UINavigationBar color globally?

set barTintColor

UINavigationBar.appearance().barTintColor = UIColor(red: 63.0/255.0, green: 172.0/255.0, blue: 236.0/255.0, alpha: 1.0)

I think you forgot to divide with 255

For turn off the translucent. In your first root controller do it as follows.

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController!.navigationBar.translucent = false
}

Swift 3 :

override func viewDidLoad() {
super.viewDidLoad()
self.navigationController!.navigationBar.isTranslucent = false
}

My output:

Sample Image

Does changing the appearance of the Navigation Bar in App Delegate override the embedded navigation bar?

I think the issue with your invisible, working back button in your 2nd view controller is the fact that you have set the navigation bar barTintColor to white, and also the tintColor to white. This gives you a white back button on a white navigation bar - making it invisible!

Try it again with these settings in your AppDelegate:

UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().tintColor = UIColor.blue

As for your refresh button on the 1st view controller - I think there's two possibilities for why you can't see it:

  • "back_button.png" is a white or transparent image, which is not visible against the white navigation bar.
  • the "back_button.png" image you specified does not exist and you're creating a button with no image at all, which (I think) would render it invisible.

How to change the background color of navigationBar

You can also change the navigation bar color for a specific view controller, this way:

extension UIViewController {
func setCustomNavigationColor(color: UIColor = .black, isTranslucent: Bool = false ){
self.navigationController?.navigationBar.barTintColor = color
self.navigationController?.navigationBar.isTranslucent = isTranslucent
}
}

call this from viewDidLoad()

setCustomNavigationColor()

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.

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.



Related Topics



Leave a reply



Submit