如何使UINavigationBar透明?虽然我希望它的栏项仍然可见。


当前回答

另一种方式,为我工作是子类UINavigationBar和留下drawRect方法空!!

@IBDesignable class MONavigationBar: UINavigationBar {


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}}

其他回答

这似乎很有效:

@implementation UINavigationBar (custom)
- (void)drawRect:(CGRect)rect {}
@end

navigationController.navigationBar.backgroundColor = [UIColor clearColor];

检查RRViewControllerExtension,它专门用于uinnavigation栏外观管理。

在你的项目中使用RRViewControllerExtension,你只需要覆盖

-(BOOL)prefersNavigationBarTransparent;

在你的视图控制器中。

在iOS5中,你可以这样做,使导航栏透明:

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];

试试下面这段代码:

self.navigationController.navigationBar.translucent = YES;

这适用于Swift 2.0。

navigationController!.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationController!.navigationBar.shadowImage = UIImage()
navigationController!.navigationBar.translucent = true