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

前端之家收集整理的这篇文章主要介绍了ios – 如何使UIImage适合整个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符合条形图的大小,因为在我的情况下它太大而且不适合整个条形.我该怎么做?

提前致谢.

解决方法

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

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,15),resizingMode: UIImageResizingMode.Stretch)

    UINavigationBar.appearance().setBackgroundImage(backgroundImage,forBarMetrics: .Default)

    return true
}

猜你在找的Xcode相关文章