iOS: remove navigationbar's background (image) - ios

At my main view, that contains a menu, I have an image on the navigationbar. When I select one of the menu items, I want to remove the background image and set text, normally.
When the second view opens, the background image still there. I tried everything, following some answers on StackOverflow, but still getting the same error.
I tried this:
- (void)viewWillDisappear:(BOOL)animated
{
UIGraphicsBeginImageContextWithOptions(CGSizeMake(36, 36), NO, 0.0);
UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
[super viewWillDisappear:animated];
}
Didn't work. Tried this:
#implementation ViewController{
UIImage *_bgNavigationBarImage;
}
- (void) viewWillAppear:(BOOL)animated {
_bgNavigationBarImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navigation-bar-home.png"] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController.navigationBar setBackgroundImage:_bgNavigationBarImage forBarMetrics:UIBarMetricsDefault];
[super viewWillDisappear:animated];
}
Didn't work too. Tried to set nil to background and still doesn't working.

Done!
Used the first method, but changing this:
[[UINavigationBar appearance] setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];
for this:
[self.navigationController.navigationBar setBackgroundImage:blank forBarMetrics:UIBarMetricsDefault];

Related

Background image of opaque navigation bar is rendered incorrectly on iPhone 6

When I set background image with method
setBackgroundImage:forBarMetrics:
it's rendered like this on iPhone 6
If i set the navigation bar to translucent, it's stretched normally.
#implementation OHCNavigationBar
- (id)initWithCoder:(NSCoder *)aDecoder {
if(self = [super initWithCoder:aDecoder]) {
[self setupGradient];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self setupGradient];
}
return self;
}
- (void)setupGradient {
UIImage *gradientImage = [UIImage imageNamed:#"navigationBarBackground.png"];
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
#end
You can set UINavigationBar background image with non repeat mode by setting edge to 0.
UIImage *gradientImage32 = [[UIImage imageNamed:#"bkg_top_header_default.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32
forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsDefault];
Update 1:
- (void)setupGradient {
UIImage *gradientImage = [[UIImage imageNamed:#"navigationBarBackground.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
[self setBackgroundImage:gradientImage forBarMetrics:UIBarMetricsDefault];
}
May this help you.
Enjoy Coding !!

iOS status bar color with custom navigationbar

I have a custom navigation bar showing a logo with an image and the status bar appears white. I'd like to be able to set the status bar color to match the greyish background of the logo.
http://i.imgur.com/wn0oOX5.png
When a cell is selected, I have the navigation bar going back to default and the status bar matches the navigation bar.
http://i.imgur.com/btQOvXn.png
LogoNavBar.m:
- (CGSize)sizeThatFits:(CGSize)size {
CGSize newSize = CGSizeMake(self.frame.size.width, 90);
return newSize;
}
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed:#"logo.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
MasterViewController.m:
- (void)viewDidLoad
{
[self.navigationController setValue:[[LogoNavBar alloc]init] forKeyPath:#"navigationBar"];
}
DetailViewController.m:
- (void)viewDidLoad
{
[self.navigationController setValue:[[UINavigationBar alloc]init] forKeyPath:#"navigationBar"];
}
I've tried:
[[UINavigationBar appearance] setBarTintColor:[UIColor *colorhere*]];
in the app delegate and elsewhere but to no luck.

Changing the UINavigationBar background depending on landscape/portrait?

I want my UINavigationBar to rotate properly within my SignatureViewController. This is currently how it looks in Portrait vs Landscapemode when rotating: http://tinypic.com/view.php?pic=2w5573a&s=5#.Uk5kgYapWrg
As you can see the UINavigationBar does not scale on the width when in landscape. My project is built as following:
rootViewController(UINavgationController) ------Modal push-----> SignatureViewController
(portrait only) (portrait/landscape)
Note that I am not using a normal push to the SignatureViewController, I am using a modal one. Just in case I wanna mention this because I am not sure if it can affect the outcome in any strange way?... I have tried different approaches to change the background of the NavigationBar depending on if it is in landscape or portrait. I will post some here below:
try nr1: Not working
- (void) viewWillAppear:(BOOL)animated {
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsLandscapePhone];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
try nr2: Not working
- (void) viewWillAppear:(BOOL)animated {
UIImage *navBarLandscape = [[UIImage imageNamed:#"navbar25g.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(x,x,x,x)];
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance]setBackgroundImage:navBarLandscape forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
}
try nr3: Not working
- (void)applicationDidChangeStatusBarOrientation:(NSNotification *)notification
{
UIView *v = [[UIView alloc]init];
int a = [[notification.userInfo objectForKey: UIApplicationStatusBarOrientationUserInfoKey] intValue];
int w = [[UIScreen mainScreen] bounds].size.width;
int h = [[UIScreen mainScreen] bounds].size.height;
switch(a){
case 4:
v.frame = CGRectMake(0,20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:1");
break;
case 3:
v.frame = CGRectMake(-20,0,w-20,h+20);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:2");
[self.view addSubview:v];
break;
case 2:
v.frame = CGRectMake(0,-20,w,h);
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"bar_nav_black.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
NSLog(#"willrotate:3");
break;
case 1:
v.frame = CGRectMake(20,0,w-20,h+20);
NSLog(#"willrotate:4");
[[UINavigationBar appearance]setBackgroundImage:[UIImage imageNamed:#"navbar25g.png"] forBarMetrics:UIBarMetricsDefault];//Have also tried with UIBarMetricsLandscapePhone...
}
Note that in try nr3 the NSLog works but it refuses to change the background of the UINavigationBar. So, any ideas what I shall do from here ? / Regards
can you please try to implement the UINavigationBar class and override one drawRect: method
it should work
#implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *navimage;
//we will check if screen width is 320 then it will be potrait
if(self.frame.size.width == 320)
{
navimage = [UIImage imageNamed: #"Your Potrait nav bar image"];
}
else
{
navimage = [UIImage imageNamed: #"Your Landscape nav bar image"];
}
[navimage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
#end
Hope it helps...
and just try this for nr1 and nr2
just change the last line of code with this..if its working or not...let me know
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; //UIViewAutoresizingFlexibleWidth

How can remove the bottom shadow of UINavigationBar?

I am using custom navigationBar within my projects, but it's giving bottom drop shadow of UINavigation bar, How we can remove it, Please provide the answer for it if any work on it.
TIA :)
The easiest way to remove the shadow underneath the UINavigationBar is to set a custom background image and then set the shadow image to a blank UIImage.
CustomViewController.m
- (void)viewDidLoad
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"Background"] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
}
In the above example, "Background" would be a PNG image in your project.
in iOS7:
for (UIView *view in self.navigationController.navigationBar.subviews) {
for (UIView *view2 in view.subviews) {
if ([view2 isKindOfClass:[UIImageView class]]) {
if (view2.frame.size.height < 1) {
[view2 removeFromSuperview];
}
}
}
}

change 3 navigation bar background with different image

i have 3 navigation controller and i want to change each background using different image. I was implement a category that extends UINavigationBar like this :
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:#"background.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];}
#end
but it make every navigation bar have a same background image. And then i try to implement code like this :
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
UIImageView *backGroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
[self.navigationController.navigationBar insertSubview:backGroundView atIndex:0];
[backGroundView release];
}
in every controller but each background just show the tintColor, not the image...what should I do???
and how if i want to do that too in tabbarController??
On each of your views, implement the following in your viewWillAppear: method
#import <QuartzCore/QuartzCore.h>
...
- (void)viewWillAppear:(BOOL)animated
{
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:#"yourImageBgHere"].CGImage;
}
Cheers,
Rog

Resources