How to make UIImage suitable for the entire size of the UINavigationBar

I need to set a background image for the columns of the UINavigationControllerentire 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 UIImagefit the size of the bar, because in my case it is too big and not suitable for the whole bar. How can i do this?

Thanks in advance.

+4
source share
2 answers

It should be automatically repeated to fill the size of the navigation bar, but if you want to stretch it, you can change the set of frames in the asset catalog:

https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/chapters/SlicinganImage.html

- :

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
}
+7

:

// Set the size hard coded
CGRect myFrame = CGRectMake(0,0,320,200); 
// Or you can get the size from the view
CGRect myFrame = backGroundView.frame

// Now assign the size to the image, I normally do this is an animation
[UIView beginAnimations:NULL context:NULL];
[UIView setAnimationDuration:0.5];
imageView.frame = myFrame;
[UIView commitAnimations];
0

All Articles