如何使UIImage适合整个UINavigationBar视图的大小

时间:2014-12-09 12:25:57

标签: ios xcode interface-builder xcode6 uinavigationbar

我需要为整个应用程序的UINavigationController条设置背景图像,所以我写了下面的代码:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {

    let backgroundImage = UIImage(named: "NavBar")
    UINavigationBar.appearance().setBackgroundImage(backgroundImage, forBarMetrics: .Default)

    return true
}

但是我需要让UIImage符合条形图的大小,因为在我的情况下它太大而且不适合整个条形图。我该怎么办?

提前致谢。

2 个答案:

答案 0 :(得分:8)

应自动重复以填充导航栏大小,但如果您想要拉伸,则可以更改设置资产目录中的切片插入内容:

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
}

答案 1 :(得分:0)

您可以轻松更改图像视图的大小:

// 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];