ios - UINavigationController 图像未在所有 iOS 设备上居中

标签 ios swift uiimage uinavigationbar

我有一个 UINavigationController 图像,我在 App DelegatedidFinishLaunchingWithOptions 中设置,如下所示:

var image: UIImage = UIImage(named: "Header")!
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: .Default)

HeaderAssets.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)

我什至插入了随机数来查看是否有任何变化,但它没有。

有人知道该怎么做吗?

我引用了这些链接:

  1. UINavigationBar custom title position

  2. Misaligned title in UINavigationBar since iOS6

  3. Center UIImage in UINavigationController

更新

在 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/

相关文章:

ios - 如何使用带有现有 UIPageViewController 的按钮以编程方式滑动 View

IOS - Google Admob - 错误域=com.google.ads 代码=1 "Request Error: No ad to show."

ios - 将 UIImage 数据上传到服务器获取内存峰值?

iphone - UIPageViewController 和 UIScrollView

iOS 关联域 - 子域似乎被忽略

swift - 将图像上传到 firebase 存储

swift - 评估 Swift 的 Bool?一行中的 true/false/nil?

iphone - 试图将 jpeg 保存到应用程序的文档目录

ios - 降低 UIImage 的质量(压缩它)

ios - 更改 UIImage 形式的页面的文本颜色