6

I want to develop UINavigationBar and also set background color for that. I have created the UINavigationBar but I have problem with setting backgroundcolor. anyone please help me. Thanks.

idmean
  • 14,540
  • 9
  • 54
  • 83
Sugan S
  • 1,782
  • 7
  • 25
  • 47

10 Answers10

14
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];

Try like this. I think it will be helpful to you.

Edit: updated the code to actually compile.

Johan Karlsson
  • 1,136
  • 1
  • 14
  • 37
Prasad G
  • 6,702
  • 7
  • 42
  • 65
14

In the new iOs this it how it works:

self.navigationController.navigationBar.barStyle  = UIBarStyleBlackOpaque;
self.navigationController.navigationBar.barTintColor =[UIColor colorAzulNavegacion];
Erusso87
  • 667
  • 1
  • 7
  • 19
11

I have to look this up every time so adding my answer here (Swift). The code below is setting this for all navigation bars in the app. You could set each of these on individual navigation bars too if you wanted to.

You can set the translucency, title text color, background color (this is called barTintColor, thanks, Apple!), and bar button item foreground color, like so:

    // Title text color Black => Text appears in white
    UINavigationBar.appearance().barStyle = UIBarStyle.Black

    // Translucency; false == opaque
    UINavigationBar.appearance().translucent = false

    // BACKGROUND color of nav bar
    UINavigationBar.appearance().barTintColor = UIColor.redColor()

    // Foreground color of bar button item text, e.g. "< Back", "Done", and so on.
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
n13
  • 6,843
  • 53
  • 40
2

You could use the tint property of the UINavigationBarto change it's color. Check this article about it. There is also UIAppearance, that allows you to change the background of every UINavigationBar of your application, which is quite powerfull in my opinion. You can check this.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
2

You can set the tint color by using navbar.tintColor = [UIColor redColor];

See the reference here: apple docs

erran
  • 1,300
  • 2
  • 15
  • 36
2

Try this:

navigationBar.tintColor = [UIColor blackColor];
ndeverge
  • 21,378
  • 4
  • 56
  • 85
san
  • 3,350
  • 1
  • 28
  • 40
2

self.navigationController?.navigationBar.translucent = false

self.navigationController?.navigationBar.barTintColor = UIColor.redColor()

self.navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent

tounaobun
  • 14,570
  • 9
  • 53
  • 75
1

You can customize a UINavigationBar with the following propertys:

  • @property(nonatomic, assign) UIBarStyle barStyle
  • @property(nonatomic, retain) UIColor *tintColor
  • setBackgroundImage:forBarMetrics:
  • @property(nonatomic, copy) UIColor *backgroundColor

For more methods and propertys please check the class reference of UINavigationBar and UIView

Pfitz
  • 7,336
  • 4
  • 38
  • 51
0
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
Jimi
  • 1,091
  • 1
  • 14
  • 19
  • Down voted because incorrect. tintColor is the color that will be used for bar button items foreground color, e.g. the "back" text, the "<" back caret, and any other button text... you want to use barTintColor to set the bg color of the bar. – n13 Aug 03 '15 at 07:29
0

Here it is in the context of doing something useful. In this case programmatically creating/configuring a Navigation Bar item and item and setting the background to black and the title to light gray.

Swift 5, iOS 15

@objc func addButtonPushed() {
    print("Add button!")
}

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

    let rightButton = UIBarButtonItem(image: UIImage(systemName: "plus"), style: .plain,
                                      target:self, action: #selector(addButtonPushed))

    let standaloneItem     = UINavigationItem()
    standaloneItem.title   = "Herding Cats"
    standaloneItem.rightBarButtonItem  = rightButton
    navBar.items           = [standaloneItem]
    navBar.delegate        = self
    navBar.barStyle        = UIBarStyle.default
    navBar.isTranslucent   = true
    navBar.barTintColor    = .black
    navBar.titleTextAttributes = [.foregroundColor: UIColor.lightGray]
    
    . 
    .
    .

}

clearlight
  • 12,255
  • 11
  • 57
  • 75