透明的导航栏ios

我正在创build一个应用程序,我已经浏览了互联网,我想知道他们是如何使这个透明的导航条如下所示:

在这里输入图像说明

我在appdelegate中添加了以下内容:

UINavigationBar.appearance().translucent = true 

但这只是让它看起来像下面这样:

在这里输入图像说明

我怎样才能使navigationBar像第一个图像一样透明

您可以将导航栏图像应用于半透明。

Objective-C的:

 [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; //UIImageNamed:@"transparent.png" self.navigationController.navigationBar.shadowImage = [UIImage new];////UIImageNamed:@"transparent.png" self.navigationController.navigationBar.translucent = YES; self.navigationController.view.backgroundColor = [UIColor clearColor]; 

Swift 3:

 self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) self.navigationController?.navigationBar.shadowImage = UIImage() self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.view.backgroundColor = .clear 

希望它可以帮助你..!

Swift解决scheme

这是我find的最好的方法。 你可以把它粘贴到你的appDelegate的 didFinishLaunchingWithOptions方法中:

斯威夫特3/4

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // Sets background to a blank/empty image UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default) // Sets shadow (line below the bar) to a blank image UINavigationBar.appearance().shadowImage = UIImage() // Sets the translucent background color UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0) // Set translucent. (Default value is already true, so this can be removed if desired.) UINavigationBar.appearance().isTranslucent = true return true } 

Swift 2.0

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. // Sets background to a blank/empty image UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default) // Sets shadow (line below the bar) to a blank image UINavigationBar.appearance().shadowImage = UIImage() // Sets the translucent background color UINavigationBar.appearance().backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0) // Set translucent. (Default value is already true, so this can be removed if desired.) UINavigationBar.appearance().translucent = true return true } 

来源: 在iOS 8.1中关于下面的图像使导航栏透明

它对我有什么作用:

  let bar:UINavigationBar! = self.navigationController?.navigationBar self.title = "Whatever..." bar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) bar.shadowImage = UIImage() bar.alpha = 0.0 

我可以这样迅速完成这个工作:

 let navBarAppearance = UINavigationBar.appearance() let colorImage = UIImage.imageFromColor(UIColor.morselPink(), frame: CGRectMake(0, 0, 340, 64)) navBarAppearance.setBackgroundImage(colorImage, forBarMetrics: .Default) 

我在UIColor类别中创build了以下实用程序方法:

 imageFromColor(color: UIColor, frame: CGRect) -> UIImage { UIGraphicsBeginImageContextWithOptions(frame.size, false, 0) color.setFill() UIRectFill(frame) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } 

设置导航条的背景属性,例如

 navigationController?.navigationBar.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 0.5) 

(如果你没有导航控制器,你可能需要改变一下,但是这应该让你知道该怎么做。)

还要确保下面的视图实际上在酒吧下面延伸。

Swift 3:透明导航栏的扩展

 extension UINavigationBar { func transparentNavigationBar() { self.setBackgroundImage(UIImage(), for: .default) self.shadowImage = UIImage() self.isTranslucent = true } } 

试试这个,它适用于我,如果你还需要支持ios7,它是基于UItoolBar的透明度:

 [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.shadowImage = [UIImage new]; self.navigationController.navigationBar.translucent = YES; self.navigationController.view.backgroundColor = [UIColor clearColor]; UIToolbar* blurredView = [[UIToolbar alloc] initWithFrame:self.navigationController.navigationBar.bounds]; [blurredView setBarStyle:UIBarStyleBlack]; [blurredView setBarTintColor:[UIColor redColor]]; [self.navigationController.navigationBar insertSubview:blurredView atIndex:0]; 

你可以通过导航栏调用导航控制器调用的实用方法和你喜欢设置的颜色。 对于透明,你可以使用UIColor类的clearColor

对于客观的c –

 + (void)setNavigationBarColor:(UINavigationController *)navigationController color:(UIColor*) color { [navigationController setNavigationBarHidden:false animated:false]; [navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault]; [navigationController.navigationBar setShadowImage:[UIImage new]]; [navigationController.navigationBar setTranslucent:true]; [navigationController.view setBackgroundColor:color]; [navigationController.navigationBar setBackgroundColor:color]; } 

对于Swift 3.0 –

 class func setNavigationBarColor(navigationController : UINavigationController?, color : UIColor) { navigationController?.setNavigationBarHidden(false, animated: false) navigationController?.navigationBar .setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) navigationController?.navigationBar.shadowImage = UIImage() navigationController?.navigationBar.translucent = true navigationController?.view.backgroundColor = color navigationController?.navigationBar.backgroundColor = color }