Header Logo is wrong size on Swift 4 / XCode 9 / iOS 11

Viewed 1027

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?

2 Answers

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

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