如何在IOS 11之前以编程方式获取安全区的高度? [英] How to get Height of Safe Area Programmatically Prior to IOS 11?
问题描述
无需使用safeAreaLayoutGuide(我的目标是IOS 9+),有什么方法可以以编程方式获取IOS中安全区域"的高度,而不必为此专门创建新视图(仅限于安全区域)?
Without using safeAreaLayoutGuide (I am targeting IOS 9+), is there any way to programmatically get the height of the "safe area" in IOS without having to create a new view (constrained to the safe area) solely for this purpose?
我无法在安全区域设置出口,因为它不是UIView ...甚至不是任何种类的类.
I can't set an outlet to the safe area because it's not a UIView... or even a class of any sort.
如果我只是在ViewController中使用self.view.height,它将太高(错误).
And if I simply use self.view.height in the ViewController, it's going to be too high (wrong).
还有其他方法吗?
推荐答案
在 UIViewController
中,您可以使用像这样的顶部和底部布局指南:
In a UIViewController
you can use the top and bottom layout guides like this:
let safeAreHeight = self.view.frame.height - self.topLayoutGuide.length - self.bottomLayoutGuide.length
对于 UIView
,您可以使用 safeAreaLayoutGuide
进行条件检查:
For UIView
you can use the safeAreaLayoutGuide
with a conditional check:
let verticalSafeAreaInset: CGFloat
if #available(iOS 11.0, *) {
verticalSafeAreaInset = self.view.safeAreaInsets.bottom + self.view.safeAreaInsets.top
} else {
verticalSafeAreaInset = 0.0
}
let safeAreaHeight = self.view.frame.height - verticalSafeAreaInset
由于运行iOS 9和10的设备没有安全区域,因此默认设置为0.0是安全的.
As devices running iOS 9 and 10 have no safe area, it is safe to default to 0.0.
这篇关于如何在IOS 11之前以编程方式获取安全区的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!