Set background color for UINavigationBar
Asked Answered
M

10

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.

Mascarenas answered 20/6, 2012 at 7:15 Comment(0)
C
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.

Camphorate answered 20/6, 2012 at 7:22 Comment(1)
don't know why but Xcode doesn't popup barTintColor when I type itAmbrosia
J
15

In the new iOs this it how it works:

self.navigationController.navigationBar.barStyle  = UIBarStyleBlackOpaque;
self.navigationController.navigationBar.barTintColor =[UIColor colorAzulNavegacion];
Justicz answered 23/4, 2014 at 14:19 Comment(1)
UIBarStyleBlackOpaque is deprecated in iOS 9Heard
C
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.

Camphorate answered 20/6, 2012 at 7:22 Comment(1)
don't know why but Xcode doesn't popup barTintColor when I type itAmbrosia
P
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()
Packaging answered 3/8, 2015 at 7:23 Comment(1)
This should be selected as the best answerBaba
A
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.

Aundrea answered 20/6, 2012 at 7:18 Comment(0)
L
2

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

See the reference here: apple docs

Lillis answered 20/6, 2012 at 7:19 Comment(0)
N
2

Try this:

navigationBar.tintColor = [UIColor blackColor];
Necromancy answered 20/6, 2012 at 7:20 Comment(0)
I
2

self.navigationController?.navigationBar.translucent = false

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

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

Infrequency answered 28/10, 2014 at 6:16 Comment(1)
Basically correct, except BlackTranslucent is deprecated.Packaging
C
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

Chore answered 20/6, 2012 at 7:21 Comment(0)
C
0
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
Chalcopyrite answered 7/11, 2012 at 13:26 Comment(1)
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.Packaging
M
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]
    
    . 
    .
    .

}

Magnification answered 2/4, 2022 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.