How to resize UINavigationBarAppearance background image

625 views Asked by At

I am trying to add a background image to my nav bar using the UINavigationBarAppearance. However, the imageView size is bigger than the navigation bar content view.. How do I fix this?

Result: Notice the imageview overlaps with the safearea

Result of navigation appearance with imageview

Heiarchy debugger Image heiarchy of nav bar

Code:

  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
  }
1

There are 1 answers

0
Mocha On BEST ANSWER

Found an alternative. Instead of using the appearance background image, I set the titleView on the navigation item.

let imageView = UIImageView(image: UIImage.checkmark)
imageView.contentMode = .scaleAspectFit
navigationItem.titleView = imageView