Changing navigation bar image - not working - ios

I'd like to change the background image (or color) of the navigation bar, but for some reason it's not working.
I've searched around, and found that on iOS 5.0+ (I'm running 6.1) this should work:
[self.navigationController.navigationBar setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
Now none of those work. Also tried with:
self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
Now I'm thinking that I'm assigning it to the wrong object, but this works:
UIImageView *titleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"scr1_title"]];
self.navigationItem.titleView = titleImage;

Use this code in your view controller to change the navigation bar background image.
if([[UINavigationBar class] respondsToSelector:#selector(appearance)]) //iOS >=5.0
{
UIImage * navBarImage = [UIImage imageNamed:#"image.png"] ;
[[UINavigationBar appearance]setBackgroundImage: navBarImage forBarMetrics:UIBarMetricsDefault];
}

You can set the tint color on the navigation bar of the navigation controller. Make sure you select the correct on as shown in the screenshot below.
Select your storyboard in the file browser.
Select the navigation controller in the storyboard.
In the Document Outline (second column) select the navigation bar of the navigation controller.
In the Utilities view (column on the right) adjust the tint color.
Click on the root view controller to the right on the navigation controller. It will pick up the new color after you click on it.

To set the image..
Write this code
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"navigationBar"] forBarMetrics:UIBarMetricsDefault];
inside
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
}

Related

Coloured Status Bar on a TabBarController with NavigationBar not working

I've read a lot of posts on here and tried most of the options mentioned, but none fix the issue for me. I have an app that is based off of a Tab Bar Controller. Each tab is a UIViewController with a Navigation Bar at the top.
Adding this code to the AppDelegate gives me an orange coloured Navigation Bar with white text, but a white status bar with Black text.
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
Reading the answers on various pages suggest adding the following to the View controller:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
Then calling this in View Did Load:
[self setNeedsStatusBarAppearanceUpdate];
This gets me a White status bar with White text, how can I now get the status bar to go orange to match my Navigation Bar??
The solution mentioned on here https://stackoverflow.com/a/19513714/505457 for those using a Navigation Controller doesn't work, I guess thats because my main controller is a Tab Bar Controller.
Anyone come across this before? Thanks in advance for any advice / suggestions you may have. I can provide a sample app if required, but its probably as quick to build one with the Tab Bar template, add a Navigation bar then paste in my code samples.
Plasma
You can find the statusBar UIVIew by it's name and tint it. Add this method to your AppDelegate.m and call it from didFinishLaunchingWithOptions:
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:#"statusBarWindow"] valueForKey:#"statusBar"];
if ([statusBar respondsToSelector:#selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
[self setStatusBarBackgroundColor:[UIColor orangeColor]];
...
}
Note: There are apps in store that use this method. So it is okay with the apple HIG policy.
Well I had almost the same problem since IOS7 , But my quick solution is to change all your views from the first one, I am refering to set up your color bar configuration in the AppDelegate.m file, I recommend to use this free framework Nab Bar Color And Gradient is very easy to use and also you will be available to set a beautiful gradient on all of your views.
See the examples in the project.
Normally, you shouldn't be adding a UINavigationBar to a UIViewController. Instead, fill your UITabBarController will UINavigationControllers.
let firstViewController = FirstViewController(nibName: nil, bundle: nil)
let firstNavigationController = UINavigationController(rootViewController: firstViewController)
let secondViewController = SecondViewController(nibName: nil, bundle: nil)
let secondNavigationController = UINavigationController(rootViewController: secondViewController)
let navigationControllers = [
firstNavigationController,
secondNavigationController
]
yourTabBarController.setViewControllers(navigationControllers, animated: false)
The same general process applies if you are using storyboards.
Right so the point BSmith11 made about adding Navigation controllers to a Tab Bar got me thinking. So I did a bit of Googling and an answer on this page, helps a lot: Tab bar controller inside a uinavigationcontroller
By having the TabBar controller as the rootViewController, then inserting a "NavigationController" between that and the normal ViewController allows me to do this:
//Fix up the Navigation bar tint and set text colours to white
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor orangeColor]];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
//Also requires setting 'View controller-based status bar appearance' to NO in .plist
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
Which is exactly what I had before, and that gives me a coloured StatusBar and the exact same colour NavBar. Now to see if I can add it in to the existing app and not break everything.
Plasma

My Navigation Bar will not appear clear.

My navigation Bar will not appear clear. I have the code below in my viewdid load. This code works in other view controllers but for some reason this current view controller still shows a white translucent bar on top. Not sure why this would work on some pages and not others. The NSLog prints UIDeviceWhiteColorSpace 0 0, which I know means clear. There are bar buttons on the navigation bar and I need them to stay, or else I would of just completely hid that navigation bar. Any tips on why this would happen, or other ways to have a clear navigation bar. Thank you.
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
NSLog(#"color: %#", self.navigationController.view.backgroundColor);
NSLog(#"color: %#", self.navigationController.navigationBar.backgroundColor);
to apply a unified behavior to your UINavigationController allover the app, it is better to change its appearance once in app delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
....
[[UINavigationBar appearance] setShadowImage:[UIImage new]];
[[UINavigationBar appearance] setTranslucent:YES];
[[UINavigationBar appearance] setBackgroundColor:[UIColor clearColor]];
...
}
The solution was putting the code into the viewWillAppear instead of viewDidLoad. Some view controllers need the navigation bar as full opacity and some needed it to be clear. Overall I got what I needed. Thank you!

Buttons on navigation bar do not pick up window tint colour

Note, I've recently come back to this and it seems to have been fixed in more recent versions of the SDK, without me having to implement anything except the code in the question. Many thanks to all who answered.
I have an Objective-C app that runs on the iPad and displays a view controller with a modal presentation style of UIModalPresentationPageSheet:
UINavigationController *editorNavigationController = [[UINavigationController alloc] initWithRootViewController:editorViewController];
editorNavigationController.modalPresentationStyle = UIModalPresentationPageSheet;
[navigationController presentViewController:editorNavigationController animated:YES completion:nil];
When this view controller is displayed the buttons in the navigation bar are purple, which I assume has been picked up from the window's tint colour, which is what I want.
Later I need to display another view controller over the top, that fills the whole window:
UINavigationController *previewNavigationController = [[UINavigationController alloc]initWithRootViewController:myPreviewViewController];
[owningViewController presentViewController:previewNavigationController animated:YES completion:nil];
The problem I have is that when myPreviewController is displayed, the buttons in the navigation bar are grey. I've tried reinstating the colour on the new navigation controller:
previewNavigationController.navigationBar.tintColor = [UIColor colorWithRed:123/255.0 green:26/255.0 blue:69/255.0 alpha:1];
but without any joy.
How can I get the buttons to have the correct colour? Can I get the new navigation controller to pick up the window tint colour automatically, or do I have to set it explicitly? Is this something to do with presenting the second navigation controller over the top of one that uses UIModalPresentationPageSheet?
Any help much appreciated! Thanks,
Paul
You can set the navigationBar translucent and transparent.
In view Will Appear:
self.navigationController.navigationBar.translucent = NO;
Than create a UIView with frame size of navigationBar (CGRectMake(0,0,self.view.frame.size.height,22) and create buttons on it with color you need.
I know, thats a crutch but should work)
You can change the appearance of the UIBarButtonItem globally so all UINavigationControllers will share the same design:
[[UIBarButtonItem appearance] setTintColor:[UIColor purpleColor]];
[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"fontName" size:16.0f],NSFontAttributeName,
nil] forState:UIControlStateNormal];
Additionally you could also change the [UINavigationBar appearance]:
//The setTintColor would tint the < Back button in the NavigationBar
[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTitleTextAttributes:
#{NSForegroundColorAttributeName:[UIColor blueColor]}];
This code can be add it before presenting the UIViewControllers or simply in the AppDelegate.m.

How to change a custom NavigationItem title and background color?

Earlier I used a navigationcontroller with this code :
self.navigationItem.title = #"News";
Now I have the problem that I no longer needed the navigationcontroller as I am using a page controller for navigation. Now I added a navigationbar however it doesn't change the title with this code anymore.
Also how can I change the background color?
IN iOS 7 use write following code in didFinishLaunchingWithOptions
if ([[UINavigationBar class] respondsToSelector:#selector(appearance)])
{
[[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed: 4.0/255.0 green:173.0/255.0 blue:214.0/255.0 alpha:1.0f ]]; //// change background color of navigationBar
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; /// set backButton color of navigation bar
[[UINavigationBar appearance] setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}]; // set title color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; /// set all barButton item color
self.navController.navigationBar.translucent = NO;// set translucent NO
}
Use as per your requirement.
So there is some magic happening when you use a navigation controller that you don't see and now that you've made the switch to justing using a navigation bar you lose that magic.
Navigation bars are responsible for managing and presenting UNavigationItems, and as such hold an array of them. A NavigationItem holds things like left and right buttons, and the title or titleView. When your navigation controller pushes a new controller, it creates a brand new UINavigation item then links it to your new view controller, and then pushes that onto navigation bars stack of nav items. That's why from within your view controller you set the title with self.navigationItem.title instead of referencing the navbar.
Basically you have to manage the bar and nav items yourself now. This should about do it for you:
_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, yOffset, CGRectGetHeight(size), height)];
_navItem = [[UINavigationItem alloc] initWithTitle:#"My navbar title"];
[_navBar setItems:#[_navItem]];
[self.view addSubview:_navBar];
Of course you'll have to manage the size a bit differently as my example comes from an app where it's being used in landscape mode.

UINavigationBar appearance setBackgroundImage Hides Status Bar

UIImage *gradientImage46 = [[UIImage imageNamed:#"navbar.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[UINavigationBar appearance] setBackgroundImage:gradientImage46
forBarMetrics:UIBarMetricsDefault];
I am using this to customize the appearance of my navigation bars in my app. However, this is causing the status bar to be solid black, showing nothing...time, carrier, battery, etc. Is this a bug, or did I implement this incorrect?
I have this running in a tab bar using MainWindow.xib method for interface. The first tab is just a navigation controller with a view controller inside it. Another tab is a Navigation Controller with a TableView Controller inside. If I go from one tab to the table view, and then back, the status bar appears.
The navbar.png is 320 x 44 pixels.
I also have this problem bringing my app from iOS 6 to iOS7, I solved by changing the code in this way:
instead
[UINavigationBar appearance] setBackgroundImage:gradientImage46
forBarMetrics:UIBarMetricsDefault];
i use
-(void) viewWillAppear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:gradientImage46 forBarMetrics:UIBarMetricsDefault];
}

Resources