3

I just updated XCode to version 9, and when running my project with no major changes, the title logo has increased its size to fill the header. Before upgrading, its size covered around 50% of the navigation bar as I intended to.

The code where I position the logo is the following:

//Logo on NavBar
        let logo = UIImage(named: "logo.png")
        let imageView = UIImageView(image:logo)
        imageView.height = (self.navigationController?.navigationBar.height)! - 25
        imageView.contentMode = .scaleAspectFit
        self.navigationItem.titleView = imageView

Here's how the logo used to (and it's supposed to) look:

enter image description here

And after the XCode update, this is how it looks:

enter image description here

Any ideas of why this might be happening?

Jacobo Koenig
  • 11,728
  • 9
  • 40
  • 75

2 Answers2

2

I've had the same problem - just appearing in iOS 11. So i've set a height and a width constraint for the imageview programmatically.

imageView.widthAnchor.constraint(equalToConstant: YOUR_WIDTH).isActive = true
imageView.heightAnchor.constraint(equalToConstant: YOUR_HEIGHT).isActive = true
1

To make the same in iOS 11, add a subView in the titleView and resize it as you like.

let imagen = UIImageView(frame: CGRect(x: -view.view.frame.width/3, y: -(view.navigationController?.navigationBar.frame.height)! / 2, width: view.view.frame.width/1.5 , height: (view.navigationController?.navigationBar.frame.height)!))
imagen.image = #imageLiteral(resourceName: "logo")
view.navigationItem.titleView = UIView()
view.navigationItem.titleView?.addSubview(imagen)
Raffista
  • 11
  • 1
  • 4
  • Can you please edit your answer so that x and y are 0, and then equal its center to that of the titleview? that way the size can be any size I choose. – Jacobo Koenig Sep 28 '17 at 18:02
  • with that code i get the UIImageView centered. change the variables to your case. – Raffista Sep 28 '17 at 19:47
  • I do too.. but when I reduce the size it stops being so. Centering on an extra call is a way to ensure that it shows up correctly regardless of the size people choose. Also it should be scaleToFit so that the logo is correctly proportioned regardless of its aspect ratio. – Jacobo Koenig Sep 28 '17 at 19:55