转换删除navigationBar边框为swift

我试图删除swift中的navigationBar边框。 这是通过在objective-c中使用以下代码完成的:

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault] 

这怎么能迅速完成呢?

我试过这个,但不工作:

 UINavigationBar.appearance().shadowImage = UIImage(named: "") UINavigationBar.appearance().setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default) 

尝试这个:

 UINavigationBar.appearance().shadowImage = UIImage() UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) 

要更改背景,文本和图标颜色,并通过外观代理删除导航栏的边框/阴影,请将此代码插入到didFinishLaunchingWithOptions:中:

 // our colors let backgroundColor = UIColor.blueColor() let foregroundColor = UIColor.whiteColor() // status bar text color (.LightContent = white, .Default = black) UIApplication.sharedApplication().statusBarStyle = .LightContent // solid or translucent background? UINavigationBar.appearance().translucent = false // remove bottom shadow UINavigationBar.appearance().shadowImage = UIImage() // background color UINavigationBar.appearance().setBackgroundImage(backgroundColor.toImage(), forBarMetrics: UIBarMetrics.Default) // controls and icons color UINavigationBar.appearance().tintColor = foregroundColor // text color UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: foregroundColor] 

注意:如您所见,我们需要将UIColor转换为UIImage ,以便您可以使用此扩展名:

 extension UIColor{ func toImage() -> UIImage { let rect = CGRectMake(0, 0, 1, 1) UIGraphicsBeginImageContextWithOptions(rect.size, true, 0) self.setFill() UIRectFill(rect) var image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } } 

像这样使用它: UIColor.redColor().toImage()

我已经使用下面的代码从应用程序中删除导航栏阴影。

  self.navigationController?.navigationBar.clipsToBounds = true