【问题标题】:Using custom image as background of Navigation bar of UINavigationController使用自定义图像作为 UINavigationController 导航栏的背景
【发布时间】:2014-12-09 03:00:24
【问题描述】:

我正在尝试使用图像作为导航栏的背景。我可以将图像放入栏中,但显示不正确。我附上了所需背景的图像,以及我得到的结果。

此外,这是我用来将图像放入条形背景的代码。

navigationController.navigationBar.setBackgroundImage(UIImage(named:"Top Bar Slice"), forBarMetrics: UIBarMetrics.Default)

编辑: 其中一个答案将图像放入代码中,但现在它在横栏上平铺。有没有办法解决这个问题?

我现在在应用委托中使用此代码:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "Top Bar"), forBarMetrics: UIBarMetrics.Default)

【问题讨论】:

标签: ios swift uinavigationcontroller uinavigationbar


【解决方案1】:

你需要覆盖UINavigationBar.appearance()

选项一:

let image = UIImage(named: "yourImage")
UINavigationBar.appearance().setBackgroundImage(image, forBarMetrics: UIBarMetrics.Default)

选项2:

UINavigationBar.appearance().setBackgroundImage(UIImage(named: "yourimage"), forBarMetrics: UIBarMetrics.Default)

您还可以在 viewController 中为导航项添加 titleView。

选项 3:

navigationItem.titleView = UIImageView(image: UIImage(named: "yourimage"))

【讨论】:

  • 我尝试了两种解决方案,但都不适合我。两者都只是在导航栏中不显示任何内容。原始代码可以将图像放在那里,但没有正确调整大小
  • 我可以在应用程序委托中使用您的代码,但现在我的图像偏离中心并重叠状态栏。有没有办法为背景图像设置框架?谢谢!
  • 不使用 .setBackgroundImage。您可以在视图控制器中将 UIImageView 添加到 viewDidLoad navigationItem.titleView = UIImageView(image: UIImage(named: "yourImage")) 中的 navigationItem.titleView 我已经编辑了我的答案。
猜你喜欢
  • 2011-03-14
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多