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


当前回答

对于Swift 3.0:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
}

其他回答

试试下面这段代码:

self.navigationController.navigationBar.translucent = YES;

c# / Xamarin解决方案

NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;

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

另一种方式,为我工作是子类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
}}

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

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

-(BOOL)prefersNavigationBarTransparent;

在你的视图控制器中。