我正试图在swift中删除navigationBar边框.这是通过在objective-c中使用以下代码来完成的:
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]]; [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]
怎么能在swift中完成?
我试过这个,但没有工作:
UINavigationBar.appearance().shadowImage = UIImage(named: "") UINavigationBar.appearance().setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
小智.. 11
试试这个:
UINavigationBar.appearance().shadowImage = UIImage() UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
小智.. 8
要更改背景,文本和图标的颜色,并且还通过删除外观代理导航栏的边框/阴影,在插入此代码didFinishLaunchingWithOptions:
的AppDelegate
:
// 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()