1

I am trying to get height of the toolbar which is provided by NavigationControleller, not added manually. I am getting a height of 44 in landscape and 32 in portrait when it is clearly the opposite of what I see/get on screen!

Here is the relevant part of my function:

// Update the layout based on the orientation
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

    // Add the constraints based on the orientation
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        println("Landscape")
    } else {
        println("Portrait")
    }

    let toolbarHeight = self.navigationController!.toolbar.frame.size.height
    let toolbarIntrinsicHeight = self.navigationController!.toolbar.intrinsicContentSize().height

    println("height = \(toolbarHeight)")
    println("Intrinsic height = \(toolbarIntrinsicHeight)")
    println("-------------")

}

This is the console output:

Landscape
height = 44.0
Intrinsic height = 44.0
-------------
Portrait
height = 32.0
Intrinsic height = 32.0
-------------

What is wrong? What is the correct way to do this?

Note: I am using simulators iPhone 5s and iPhone 6. iPhone 6 plus has height 44 in both orientations.

rgamber
  • 5,749
  • 10
  • 55
  • 99
  • Check the following link http://stackoverflow.com/questions/19576157/uinavigationbar-rotation-and-auto-layout – casillas Mar 27 '15 at 15:57
  • No, I have already seen that and it does not help. The problem there is that the navigation bar has been added manually (which is not what I am doing as mentioned above), and the poster needs to change its size programatically. It is a different problem IMHO. – rgamber Mar 27 '15 at 16:03
  • 2
    Any method with the word "will" in it means it hasn't happened yet. You should look for the "did" keyword so your code runs after it has happened. – Zane Helton Mar 27 '15 at 16:04
  • That makes sense! I must be getting the dimensions from the previous orientation. How did I not notice this!! There is no `viewDidTransitionToSize`, but there should be something. Thanks for pointing this out. – rgamber Mar 27 '15 at 16:05

1 Answers1

1

viewWillTransitionToSize is basically saying that is not transitioned yet, so you'll get the values before the transformation, that's why you get the values the other way around. For example set an action to a button that prints the exact same thing from viewWillTransitionToSize you'll see that you'll get the correct values.

Marius Fanu
  • 6,589
  • 1
  • 17
  • 19