How to ovverride UINavigationBar appearance if already customized - ios

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.

Related

UINavigationBar custom background image applied in one view controller and then affecting every single view controller in the app

I am working with themes in my app. Each theme comes with a custom navigation bar. As a default, I have applied a custom navigation bar in my AppDelegate:
[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
UIImage *navBackgroundImage = [UIImage imageNamed:#"purplynav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
If I go to a specific Table View Controller in my app and in the viewWillAppear, I put:
if ([self.selectedTheme isEqualToString:#"Blur"])
{
UIImage *navBackgroundImage = [UIImage imageNamed:#"pastelpinknav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
}
The "pastelpinknav" suddenly gets applied to every single navigation bar in my app, even though I'm only setting it in this one Table View Controller.
How do I set it so that the navigation bar only applies to this one view controller when set, but is still set to default in the App Delegate when a theme is not set?
Thanks in advance!
You have to change again the navigationBar image in viewWillDisappear
UIImage *navBackgroundImage = [UIImage imageNamed:#"purplynav.png"];
[[UINavigationBar appearance] setBackgroundImage:navBackgroundImage forBarMetrics:UIBarMetricsDefault];
Because you are using [UINavigationBar appearance] it will commonly changes the UINavigationBar appearance.
From your view controller call:
[[UINavigationBar appearanceWhenContainedIn:[self class], nil] setBackgroundImage:navBackgroundImage
forBarMetrics:UIBarMetricsDefault];
Explenation:
This will change the foo color of the entire app:
[[UINavigationBar appearance] setFooColor:#"blue"]
In order to change appearance of specific container (i.e your view controller), you need to implement UIAppearanceContainer and then call (UIViewController already implements it):
[UINavigationBar appearanceWhenContainedIn:(Class<UIAppearanceContainer>), ..., nil]

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

Issued changing the navigationbar image?

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).

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?
To Set Navigationbar Background Color:
[[UINavigationBar appearance] setBarTintColor:[UIColor orangeColor]];
To set Navigationbar Tint Color:
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
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.
[[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

Missing drop shadow under UINavigationBar in combination with UITableView

When I try to add a custom background to a navigation bar, their is no drop shadow on top of the table view. In the subviews everything is fine.
Here is my code I use.
// Create resizable images
UIImage *gradientImage44 = [[UIImage imageNamed:#"navbar"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44
forBarMetrics:UIBarMetricsDefault];
// Customize NavBar Shadow
[[UINavigationBar appearance] setShadowImage:nil];
Not existing drop shadow
Existing drop shadow
Update:
Found this answer, but does not work. I have done it exactly in the same way :-(.
I fixed it, by reimplementing the the navigation controller. The view hierarchy looked like this.
UIView
+- Navigation View
+- Table View
I changed my UIView to an table view and added this to the UINavigation Controller. That fixed the missing shadow issue.
replace
[[UINavigationBar appearance] setShadowImage:nil];
with
[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:#"SomeShadowImage"]];
And it's for iOS 6.0 and later.
I followed #madcat's answer. In my case the UITableView or UITableViewController (embedded in a Container View) had to be above the Navigation Bar (dragged onto view) in the hierarchy. Like that the Navigation Bar was drawn with the shadow after/over the UITableView/Container

Resources