Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the UIImage fit the whole UINavigationBar view's size

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.

like image 613
FrozenHeart Avatar asked Dec 09 '14 12:12

FrozenHeart


1 Answers

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
}
like image 160
Steve Avatar answered Sep 20 '22 17:09

Steve