简体   繁体   English

UINavigationController工具栏高度在方向更改时

[英]UINavigationController toolbar height on orientation change

I am trying to get height of the toolbar which is provided by NavigationControleller , not added manually. 我正在尝试获取NavigationControleller提供的toolbar高度,而不是手动添加。 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! 当与我在屏幕上看到/看到的图像明显相反时,我的横向风景高度为44,肖像纵向高度为32

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. 注意:我正在使用模拟器iPhone 5s和iPhone6。iPhone6 plus在两个方向上的高度均为44。

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. viewWillTransitionToSize基本上说还没有过渡,因此您将在转换之前获取值,这就是为什么要反过来获取值的原因。 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. 例如,将一个操作设置为一个按钮,该按钮会从viewWillTransitionToSize打印完全相同的内容,您会看到将获得正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM