Make UINavigationBar transparent
If anybody is wondering how to achieve this in iOS 7+, here's a solution (iOS 6 compatible too)
In Objective-C
[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];self.navigationBar.shadowImage = [UIImage new];self.navigationBar.translucent = YES;
In swift 3 (iOS 10)
self.navigationBar.setBackgroundImage(UIImage(), for: .default)self.navigationBar.shadowImage = UIImage()self.navigationBar.isTranslucent = true
In swift 2
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)self.navigationBar.shadowImage = UIImage()self.navigationBar.translucent = true
Discussion
Setting translucent
to YES
on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar
documentation. I'll report here the relevant fragment:
If you set this property to
YES
on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.
In iOS5 you can do this to make the navigation bar transparent:
nav.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)];[nav.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault]; [img release];
From IOS7 :
self.navigationController.navigationBar.translucent = YES;self.navigationController.navigationBar.shadowImage = [UIImage new];self.navigationController.view.backgroundColor = [UIColor clearColor];[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];