我有一个 UINavigationController
图像,我在 App Delegate
的 didFinishLaunchingWithOptions
中设置,如下所示:
var image: UIImage = UIImage(named: "Header")!
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: .Default)
Header
是 Assets.xcassets
中的文件,其中包含 2x 和 3x 大小的图像。我拿出了 1x 尺寸,因为我读到它是必要的,而且无论哪种方式都没有什么区别。在 iPhone 6 上,图像完全居中。在 iPhone5 上,它更靠右。当我将上述代码放入文件的 viewDidLoad 方法中时,我得到一个空白标题。
我还尝试调整图像大小:
var image: UIImage = UIImage(named: "Header")!
image.resizableImageWithCapInsets(UIEdgeInsetsMake(0, -260, 0, 0))
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: .Default)
我什至插入了随机数来查看是否有任何变化,但它没有。
有人知道该怎么做吗?
我引用了这些链接:
更新
在 iPhone5 上仍然无法将导航栏上的 UIImage 居中。
最佳答案
下面是我在应用程序委托(delegate)中添加的代码片段。 这非常适合我。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
//Cutomize navigation bar
let navBgImage:UIImage = UIImage(named: "dummy_navigation_bg_image")!
UINavigationBar.appearance().setBackgroundImage(navBgImage, forBarMetrics: .Default)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()];
return true
}
关于ios - UINavigationController 图像未在所有 iOS 设备上居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018043/