Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to make Navigation Bar totally transparent in iOS6

I was using the following code to make my Navigation Bar transparent in iOS5:

const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];

Upgraded to iOS6 and the Navigation Bar is still transparent but now has a thin black line underneath it. How can I make the Navigation Bar totally transparent?

I have also tried all of the following:

self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];

Thanks in advance.

like image 463
Byron Cox Avatar asked Nov 12 '12 05:11

Byron Cox


People also ask

How do I make my navigation bar clear?

You need to do three things to make a navigation bar transparent. Set background image to non-nil empty image ( UIImage() ). Set shadow image to non-nil empty image ( UIImage() ). Set isTranslucent to true .

How do I make my navigation bar opaque?

Go to attribute inspector of for Navigation bar and remove check mark from translucent.

How do I change the navigation bar on my Iphone?

A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.


3 Answers

Solved. iOS6 has added a drop shadow to the Navigation Bar. So the masking code that I was using with iOS5 still works fine - I just need to add

if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)]) 
{ 
[self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]]; 
} 

to get rid of the drop shadow.

like image 180
Byron Cox Avatar answered Nov 03 '22 02:11

Byron Cox


    self.navigationController.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
    const float colorMask[6] = {222, 255, 222, 255, 222, 255};
    UIImage *img = [[UIImage alloc] init];
    UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];

    [self.navigationController.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
//remove shadow
    [[UINavigationBar appearance] setShadowImage: [[UIImage alloc] init]];
like image 38
Robert Varga Avatar answered Nov 03 '22 02:11

Robert Varga


 if ([self.navigationController.navigationBar respondsToSelector:@selector(shadowImage)])           
 { 
    [self.navigationController.navigationBar setShadowImage:[[[UIImage alloc] init] autorelease]]; 
    // autorelease is necessary, or else [[UIImage alloc] init]'s retainCount is 2.
 }
like image 28
Sarah Zuo Avatar answered Nov 03 '22 00:11

Sarah Zuo