Custom UINavigationBar Background Working on all Navigation Controllers Except One - ios

I have a series of view controllers connected by a root navigation controller. All of the view controllers are table views, except one which is a view controller.
I have a custom navigationBar image which I've applied in the App Delegate and it works great everywhere, except this one view controller. With this, when the segue happens (Push), it almost super imposes the custom background with another image and the result is a darker uinavigationBar.
My code in the App Delegate is:
UIImage *nav = [UIImage imageNamed:#"Purple.png"];
[UINavigationBar appearance] setBackgroundImage:nav forBarMetrics:UIBarMetricsDefault];
Does anyone have any ideas for what I can do to ensure this view controller maintains the correct custom UINavigationBar?
In the viewDidLoad of this view, I've even tried to set this:
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"Purple.png"] forBarMetrics:UIBarMetricsDefault];
I also tried this in the viewWillAppear but nothing - still that darker image.
Any thoughts would be greatly appreciated!

you wrote this wrong
UIImage *nav = [UIImage imageNamed:"#Purple.png#];
replace that with this
UIImage *nav = [UIImage imageNamed:#"Purple.png"];

Related

Show transparent UINavigationbar will see black bar when swipe the viewController

I have a UINavigationController with 3 viewControllers. We know the three viewControllers share a common navigationBar.If I want to set the navigationBar totally transparent. I can put the code in viewWillAppear:
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setBarTintColor:[UIColor clearColor]];
self.navigationBar.translucent = YES;
and set it back in viewWillDisappear:
[self setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self setShadowImage:nil];
[self setBarTintColor:THEME_COLOR];
self.translucent = NO;
I want to set the UINavigationBar translucent only in viewControllerB, so I put the code in viewControllerB. However, when I popToViewController B, I can see a black bar in the top right of the screen. Since the viewWillAppear is invocated. It seems can not be solved in my case.
I come out with some methods:
use different UINavigationBar.
use different UINavigationController. But UINavigationController can not push a new UINavigationController
Custom UIView like UINavigationBar.
I think above methos is more complicated。
Any ideas thanks!
That black color you see is the background color of main window. You can set background image or color to your main window from AppDelegate didFinishLaunchingWithOptions method (That's totally depends with your design of the view controller B) so that you won't see any difference.
Or else
Simply you can use viewDidAppear instead of using viewWillAppear, but that will have little flick though.

Can't set my navigation bar background image

I want to change my table view navigation bar background image to some png, i created the nav bar png with the exact sizes of a default nav bar (I checked in interface builder).
This is my code (in my table view controller class, viewDidLoad method):
UINavigationBar *navBar = self.navigationController.navigationBar;
UIImage *image = [UIImage imageNamed:#"nav.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Shouldn't this work..?
this is my table view settings in the interface builder if this help:
the top one is what i suppose to get and the bottom one is what im getting:
thanks!
UIImageView *backImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"nav.png"]];
[self.navigationController.navigationBar addSubview:backImgView];
Try to add Image in custom view
Why not use the existing nav bar? Don t think you need to create a new one... This code is working for me :
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"nav.png"] forBarMetrics:UIBarMetricsDefault];
Your TableViewController must be "embadded" in a navigation controller a.k.a connected to other ViewControllers as push segue... Then you get your nav bar automatically instead of creating one... I mean you create a nav bar and never add it to the view isnt it?

Settings the title from inside a UITableView

I'm working on a simple application with 3 tabs at the moment, the first tab is a UITableViewController, the other 2 are UIViewControllers.
Now from inside both UIViewControllers, I can set self.title and the title of the navbar will change.
But if I try this from inside the UITableViewController, this doesn't work, why is this?
I'm also trying to set a custom background for the navigation bar, after checking some tutorials, I found that this should work, but it really doesn't
UIImage *image = [UIImage imageNamed:#"bgNavigationBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
self.navigationItem.title=#"Your Title";
should work, just put it in
- (void)viewDidLoad {
}
of the UITableViewController

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.

Remove Background Image on UINavigationBar

My app uses a navigation controller with an image on in the navigation bar of the home page. The background image is done by putting
[navigationController.navigationBar setBackgroundImage:navImage forBarMetrics:UIBarMetricsDefault];
[_window addSubview:navigationController.view];
in the app delegate. The nav bar displays this image fine.
Every other view will not have an image in the navigation bar. Therefore, I need to remove this background image when any other view controller is pushed. Does anyone know how this can be done?
There are many answers online about adding or changing a nav bar image, but this question is a little bit different. Thank you in advance.
To remove UINavigationBar background in iOS 5 or later, you should do this:
[navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
Hope it helps
I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.
#implementation MyViewController {
UIImage *_defaultImage;
}
- (void)viewWillAppear:(BOOL)animated {
_defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"bar.png"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated {
[self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}
You can set background image with blank image created in runtime.
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
Creating blank image in runtime:
How to create a blank transparent png in Objective-C?
Nothing here have worked for me. After searching for hours, the only thing that worked was following a source code example provided by Apple.
I placed the following code and it worked and removed the navigation bar image from other view controllers finally!!
// Reset everything about navigation bar. (Other flows' background image used to show up on another view controller)
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController!.navigationBar.setBackgroundImage(nil, for: .compact)
self.navigationController!.navigationBar.barTintColor = nil
self.navigationController!.toolbar.barTintColor = nil
self.navigationController!.toolbar.isTranslucent = true
Hope this helps someone.
Cheers
It looks like the easiest way to do this is to:
1) set the alpha of the navigation bar on the home page to 0.000001
2) put the background image in a UIImageView "behind" the navigation bar (which is now clear)
3) use viewWillAppear / viewWillDisappear to set the navigation bar's opacity to either 1 or 0.00001 when views are pushed / popped.

Resources