如何获得topLayoutGuide的高度?

时间:2015-05-17 06:30:48

标签: ios swift

 moviePlayer = MPMoviePlayerController(contentURL: url)
 moviePlayer.view.frame = CGRect(x: 0, y:{layoutguide.height}, width:
 self.view.frame.width, height: 300)
 self.view.addSubview(moviePlayer.view)

viewcontroller有一个名为“topLayoutGuide”的属性,但似乎不是我想要的。

我知道如何在故事板中实现, 代码 ,我无法获得顶级布局指南的高度。我搜索了几个小时什么都没有。

enter image description here

enter image description here

以下是希望。

enter image description here

3 个答案:

答案 0 :(得分:46)

您可以将topLayoutGuide值作为其length属性:

// Inside your viewController
self.topLayoutGuide.length

因为它是单个值(即:它没有高度和宽度),所以它们只称它为length。同样适用于bottomLayoutGuide。

希望这有帮助

还有一件事需要提及,在这个属性的苹果文档中:

  

//在不使用自动布局的情况下,这个值在-viewDidLayoutSubviews中引用是安全的,或者在调用super之后在-layoutSubviews中引用

在这两个函数中使用此属性将获得准确的值,因为布局已初始化。如果您在viewDidLoad函数中使用它,则此属性将为0.

答案 1 :(得分:20)

如果有人正在寻找如何计算适用于iOS 11的SafeLayoutGuide插图的高度,因为现在不推荐使用“顶部和底部布局指南”,您可以在以下位置找到它:

producerB

请注意,顶部和底部布局指南是ViewController的一部分,现在SafeLayoutGuide是ViewController主视图的一部分。

答案 2 :(得分:5)

这将得到视图控制器view部分的长度(以磅为单位),该部分由半透明或透明的UIKit条重叠。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    var topSpace:CGFloat?
    var bottomSpace:CGFloat?
    if #available(iOS 11.0, *) {
        topSpace = self.view.safeAreaInsets.top
        bottomSpace = self.view.safeAreaInsets.bottom
    } else {
        topSpace = self.topLayoutGuide.length
        bottomSpace = self.bottomLayoutGuide.length
    }
}