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


当前回答

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

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

-(BOOL)prefersNavigationBarTransparent;

在你的视图控制器中。

其他回答

下面的代码扩展了为这个线程选择的顶部答案,以消除底部边框并设置文本颜色:

The last two coded lines of this code set transparency. I borrowed that code from this thread and it worked perfectly! The "clipsToBounds" property was code I found which got rid of the bottom border line with OR without transparency set (so if you decide to go with a solid white/black/etc. background instead, there will still be no border line). The "tintColor" line (2nd coded line) set my back button to a light grey I kept barTintColor as a backup. I don't know why transparency would not work, but if it doesn't, I want my bg white as I used to have it let navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.tintColor = UIColor.lightGray navigationBarAppearace.barTintColor = UIColor.white navigationBarAppearace.clipsToBounds = true navigationBarAppearace.isTranslucent = true navigationBarAppearace.setBackgroundImage(UIImage(), for: .default) navigationBarAppearace.shadowImage = UIImage()

这适用于Swift 2.0。

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

你的意思是完全透明,还是使用照片应用程序中看到的半透明黑色?后者你可以通过设置它的barStyle属性为uibarstyleblack半透明来实现。前……我不太确定。如果您希望其中的项仍然可见,您可能必须在栏的视图层次结构中进行一些挖掘,并删除包含其背景的视图。

这似乎很有效:

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

navigationController.navigationBar.backgroundColor = [UIColor clearColor];

试试下面这段代码:

self.navigationController.navigationBar.translucent = YES;