Convert remove navigation border
I am trying to remove the border of a navigationBar in swift. This is done with the following code in objective-c:
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]
How can this be done in swift?
I tried this but didn't work:
UINavigationBar.appearance().shadowImage = UIImage(named: "")
UINavigationBar.appearance().setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)
+3
user3926776
source
to share
3 answers
To change the background color, text and icons, and also remove the border / shadow of the navigation bar through the appearance proxy, paste this code in didFinishLaunchingWithOptions:
from 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]
Note. ... As you can see, we need to convert UIColor
to UIImage
so that you can use this extension:
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
}
}
Use it like this: UIColor.redColor().toImage()
+8
source to share