0

我正在尝试使用 UINavigationBarAppearance 将背景图像添加到我的导航栏。但是,imageView 的大小比导航栏内容视图大。我该如何解决这个问题?

结果:注意图像视图与安全区域重叠

使用 imageview 的导航外观结果

层次结构调试器 导航栏的图像层次结构

代码:

  private func setupNavBar() {
    navigationItem.largeTitleDisplayMode = .never
    guard let navigationController = navigationController else { return }
    let appearance = navigationController.navigationBar.standardAppearance.copy()
    appearance.configureWithTransparentBackground()
    appearance.backgroundImage = UIImage.checkmark
    appearance.backgroundImageContentMode = .scaleAspectFit
    appearance.backgroundColor = .red
    navigationController.navigationBar.standardAppearance = appearance
  }
4

1 回答 1

0

找到了替代方案。我没有使用外观背景图片,而是在导航项上设置了 titleView。

let imageView = UIImageView(image: UIImage.checkmark)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView
于 2020-07-17T00:20:34.503 回答