如何为导航栏 AppDelegate 设置图像 Swift

How to set image for Navigation bar AppDelegate Swift

我在 SO 上找到了一堆链接,这些链接展示了如何将导航栏设置为 Objective-C 中的图像,但 none 在 swift 中。我找到的那个没有帮助:UINavigationBar setBackgroundImage in AppDelegate with Swift

AppDelegate 中的 didFinishLaunchingWithOptions 中,我试图用 swift 执行此操作但无济于事:

    let headerImage = UIImage(named: "header.png")
    UINavigationBar.appearance().setBackgroundImage(headerImage, forBarMetrics:.Default)
     UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default

    var navigationVC:UINavigationController = UINavigationController(rootViewController: tableVC)
    let frame = UIScreen.mainScreen().bounds
    window = UIWindow(frame: frame)

    window!.rootViewController = navigationVC
    window!.makeKeyAndVisible()

如何将导航栏设置为 swift 中的图像?

提前致谢。

您正在使用视网膜图像,但 Xcode 不知道,因为它没有包含 @2x 的文件名。

因此,如果您将图像重命名为 header@2x.png,它会起作用。

您可能需要考虑为图像使用资产目录以避免此类情况。