【问题标题】:How to set image for Navigation bar AppDelegate Swift如何为导航栏 AppDelegate Swift 设置图像
【发布时间】:2016-02-11 05:44:07
【问题描述】:

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

didFinishLaunchingWithOptionsAppDelegate 中,我正在尝试快速执行此操作但无济于事:

    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()

如何快速将导航栏设置为图片?

提前致谢。

【问题讨论】:

  • 你的代码很好。问题在于图像本身。您确定将其添加到目标中吗?调试headerImage 并确保它不是nil
  • 如何确保已将其添加到目标中? postimg.org/image/pw1x4olrj 大概通过这张图片header.png 是在正确的位置......当在headerImage 上设置断点时,应用程序下面的行崩溃给我一个 llDB 错误。 @乔恩
  • 选择您的图像并打开实用程序视图。确保在 Target Membership 下有一个复选标记。这是截图:postimg.org/image/x0027l3nh
  • 同样的事情...postimg.org/image/6tkqp1wm5
  • 你尝试过其他图片吗?

标签: swift uinavigationbar swift2 appdelegate


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多