Issued changing the navigationbar image? - ios

Currently, I am able to change the tint of the my UINavigationBar by
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:74/255.0f green:74/255.0f blue:74/255.0f alpha:1.0f];
However when I try to implement the following code to change the image of the UINavigationBar all together, I see no results..
UIImage *navImageBackground = [[UIImage imageNamed:#"texturedNav"] //This being my .png image
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navImageBackground
forBarMetrics:UIBarMetricsDefault];
What am I doing wrong here?

You're probably calling the appearance delegate after the navigation bar is already on screen so your request is being 'ignored'. Or, at least, not causing a UI update.
Set the appearance before the navigation bar is shown. Or, force a UI refresh by hiding and showing the navigation bar (without animation).

Related

Remove status bar without making Navigation Bar shorter Objective-C

I'm making an app in which I have a navigation bar that contains a custom background image. It looks like this.
I want to remove the status bar (as it covers part of the Nav Bar image), however when I do it shortens the NavBar, like so:
I want to find a way to get rid of the status bar without shortening the nav bar and causing the image in the navBar to come all out of proportion.
Here is the code I use to set the background image of the NavBar.
[[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:#"navigationBar"] stretchableImageWithLeftCapWidth:0 topCapHeight:0] forBarMetrics:UIBarMetricsDefault];
Thanks!
Edit: Please note that I attempted to just change the height of the Navigation Bar, however the background image remained out of proportion.
UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourLogoImage"]];
[logoImageView setFrame:CGRectMake(0.0f, 0.0f, 200.0f, 44.0f)];
logiImageView.contentMode = UIViewContentModeCenter;
self.navigationItem.titleView = logoImageView ;
Will fix your UIImage stretching.
EDIT:
or
self.navigationController.navigationItem.titleView = logoImageView;

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];
}

transparent image in UINavigationBar

i have made a simple design in photoshop
when i tried to put it in Xcode i had some problems with NavigationBar
i used this code to change the UINavigationBar background image (a PNG transparent image):
UINavigationBar *navBar = [[self navigationController]navigationBar];
[navBar setBackgroundImage[UIImageimageNamed:#"navBar2.png"]forBarMetrics:UIBarMetricsDefault]
everything works fine but the problem is that the image appears in the navigationBar without transparency.
please someone help me to fix the transparency issue i search everywhere without any satisfied answer
NB! Im using IOS 5
You might try setting the navigation bar to translucent.
[navBar setTranslucent: YES];
Do you have it in your Storyboard? Try change the style of your navigation bar into black translucent.
What you can do is set the background color of the view of the navigationController the same as the backgrouond color of the main view.
//Set your view's background color
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"view_background"]]];
//Set your custom image for the navigationController
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: #"navigation_bar_image"] forBarMetrics:UIBarMetricsDefault];
//And then set the background color of the navigationController the same as the one of the view
[self.navigationController.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"view_background"]]];
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setTranslucent:YES];
And make sure you set up tint color to default.

How do I edit the background color of backBarButtonItem (UINavigationItem) ? iOS 4.3 issue

I am trying to edit the background behind the backBarButtonItem as it insists on remaining white. I have edited the background color of the entire navigation bar but to no avail.
It only seems to affect iOS4 however, seeing as the:
[self.navigationController.navigationBar setBackgroundImage:navBarBackground forBarMetrics:UIBarMetricsDefault];
code works on iOS5. I have also tried to edit the navigation bar using this method also:
#implementation UINavigationBar (UINavigationBarCategory)
-(void)drawRect:(CGRect)rect{
UIImage *img = [UIImage imageNamed:#"bg_actionbar_edit.png"];
[img drawInRect:rect];
}
#end;
however this code results in all of my navigation bars to change, which is something that I do not want. Is there any simple code that I could use to simply change the BG colour of a buttonItem based on the navigation bar?
Set the tint of your navigation bar to change the color of the navigation bar button items:
self.navigationController.navigationBar.tintColor = [UIColor grayColor];

How to ovverride UINavigationBar appearance if already customized

In the application delegate within my application I call the following method:
- (void)customizeAppearance
{
UIImage *gradientPortrait = [[UIImage imageNamed:#"gradient_portrait"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientLandscape = [[UIImage imageNamed:#"gradient_landscape"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientPortrait
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientLandscape
forBarMetrics:UIBarMetricsLandscapePhone];
}
This code allow to customize all the navigation bars within the application. Each bar becomes green since the image I use is green.
Now my goal is to ovveride the above configuration for a specific navigation bar.
In particular, during application lifecycle, I open a modal controller using UIModalPresentationFormSheet presentation style. This controller is presented within a UINavigationController. Since I need also to display the navigation bar attached with that UINavigationController, I would like to know how it is possible to customize that bar, without changing the global configuration I set in the application delegate.
I've tried to set both the tintColor property of the navigation bar (presented modally) to [UIColor blackColor] and the barStyle to UIBarStyleBlack, but they don't work. Only barbutton items are affected.
Thank you in advance.
P.S. I'm using iOS 5
First of all if you don't mind your image stretched you don't need to use the resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) method.
UIImage *gradientPortrait = [UIImage imageNamed:#"gradient_portrait"];
UIImage *gradientLandscape = [UIImage imageNamed:#"gradient_landscape"];
UIImage *someOtherImage = [UIImage imageNamed:#"someOtherImageName"];
Then, to achieve what you want:
Make sure you subclass the ViewController on with the custom Navigationbar will appear
Use the following to add the default image to all of your Navigation Bars
[[UINavigationBar appearance] setBackgroundImage:gradientPortrait forBarMetrics:UIBarMetricsDefault];
Use the following to add the specific image to navigationbars wich will appear above the subclassed ViewControllers
[[UINavigationBar appearanceWhenContainedIn:[YOURSUBCLASS class], nil] setBackgroundImage:someOtherImage forBarMetrics:UIBarMetricsDefault];
(you can call these methods from somewhere in your app delegate)
btw, if you just want to change the tint color you could just use the tintColor Property instead of all the images.
for more info check out the appearance sections of: UINavigationBar Class Reference
Subclass UINavigationBar with your custom class and se different appearance on it. Then use your custom class on that particular place where you what to have different look.

Resources