How to make navigation bar transparent in iOS 7? [

2019-02-03 04:23发布

问题:

This question already has an answer here:

  • How to draw a transparent UIToolbar or UINavigationBar in iOS7 5 answers

Is there a way to make the bar of a navigation controller totally transparent?

What i've tried:

[self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor clearColor]];
[self.navigationController.navigationBar setAlpha:0.0];

but the bar's appearance does not change, and stays white translucent. I'm trying to get visible the bar items, but not the bar itself. Can someone point me in the right direction? Thanks

回答1:

If anybody is wondering how to achieve this in iOS 7, here's a solution (iOS 6 compatible too)

[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;

Setting translucent to YES on the navigation bar does the trick, due to a behaviour 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.

Reference from: Make UINavigationBar transparent



回答2:

You can make Navigation Bar Transparent with following code


[self.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationBar setShadowImage:[UIImage new]];
[self.navigationBar setTranslucent:YES];