1

In my app I want to change the titleView of my Navigation Controller to a custom ImageView but I have a very strange problem. I basically just can`t set the frame of my ImageView.

This is my code:

func NavBarEinrichten(){

    let logo = UIImage(named: "Notennamen für Griffe 1")!
    let imageView = UIImageView(image:logo)
    imageView.contentMode = .scaleAspectFit

    //To show the problem better
    imageView.backgroundColor = UIColor.red


    let navController = navigationController!

    let bannerWidth:CGFloat = 20
    let bannerHeight:CGFloat = navController.navigationBar.frame.size.height

    let bannerX = bannerWidth / 2 - logo.size.width / 2
    let bannerY = bannerHeight / 2 - logo.size.height / 2

    imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
    self.navigationItem.titleView?.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)

    navigationItem.titleView = imageView

    print(imageView.frame, "imageView frame")

}

The print statement prints:

(0.0, 0.0, 357.0, 142.0) imageView frame

Which is right, because it looks like this when i run it, but not like it should be. Because I want the width to be 20.

In the simulator it looks like this:

Actual appearance

I tried to call this method in viewDidLoad(), viewWillAppear()and viewDidLayoutSubviews() Every time with the exact same outcome...

Can you help me?

Shamim Hossain
  • 1,690
  • 12
  • 21
Quintus
  • 35
  • 1
  • 10

0 Answers0