jdog
jdog

Reputation: 10759

How do you set the background color of a UINavigationbar programatically?

I am using a cocoa iOS component from cocoacontrols.com, which is NOT using storyboards. This class creates a new modal view and the modal view has a NavigationBar at the top. I am trying to get the color of the navigationBar to change. In all my other views, which are storyboard based, I use a base class of STBaseViewController which sets the navigationBar as white.

I have changed this cocoacontrol class to be a STBaseViewController instead of a UIViewController, but it still is not changing my background to white.

@interface BZPasscodeFieldController : STBaseViewController

In the BZPasscodeFieldController, which they don't make any reference to a navigationbar, so I am not sure how its even showing up (again it has no storyboard's only xibs and they don't have navigationbars)?

Anyway, does anyone know how to make the navigationbar background color white programatically?

Further, in all my storyboard viewControllers that have a UINavigationBar, I have added a UIButton over the area where the title goes, so that I can easily change the font/style and also make it clickable. I need to add this same UIButton to the uinavigationBar of this BZPasscodeFieldController created programatically. How would I go about doing that?

Upvotes: 2

Views: 5026

Answers (4)

iAnkit
iAnkit

Reputation: 1960

To Set Navigationbar Background Color:

[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];

To set Navigationbar Tint Color:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

Upvotes: 3

ambientlight
ambientlight

Reputation: 7332

For your question about the button on the place of title of navigationBar. Each navigationBar has a titleView property, you can assign any view to it. For example take a look at this method, you can call it in your BZPasscodeFieldController:

- (void) setNavigationBarTitleButton
{
    UIButton* centralButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 44)];
    [centralButton setImage:navigationCentralButtonImage forState:UIControlStateNormal];
    [centralButton setShowsTouchWhenHighlighted:TRUE];
    [centralButton addTarget:self action:@selector(goHigher) forControlEvents:UIControlEventTouchDown];

    self.navigationItem.titleView = centralButton;

}

For your previous question the answers provided already are all correct, calling them in the right place(for example in AppDelegate's application:didFinishLaunchingWithOptions: should make it work well, unless something is messed up in your custom view controller classes.

Upvotes: 1

meda
meda

Reputation: 45490

[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

Upvotes: 1

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];

Upvotes: 1

Related Questions