I need to set the background image for UINavigationController
's bars of the whole application, so I wrote the following code:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
let backgroundImage = UIImage(named: "NavBar")
UINavigationBar.appearance().setBackgroundImage(backgroundImage, forBarMetrics: .Default)
return true
}
However I need to make the UIImage
fit the bar's size, because in my case it's too big and doesn't fit the whole bar. How can I do it?
Thanks in advance.
It should automatically repeat to fill the navigation bar size but if you want it to stretch you can change set the slicing insets in Asset catalog:
https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/chapters/SlicinganImage.html
or in code something like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let backgroundImage = UIImage(named: "NavBar")?.resizableImageWithCapInsets(UIEdgeInsetsMake(0, 15, 0, 15), resizingMode: UIImageResizingMode.Stretch)
UINavigationBar.appearance().setBackgroundImage(backgroundImage, forBarMetrics: .Default)
return true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With