我有一个函数可以计算屏幕高度的 1/3。这是实现:
struct Offsets {
static let singlePixel = (1.0 / UIScreen.main.scale)
static func thirdScreenHeightExceptTabAndNavBar() -> CGFloat {
let screenHeight = UIScreen.main.bounds.size.height
let screenHeightExceptStatusAndNavAndTabBar = screenHeight - Offsets.statusBarHeight() - Offsets.navBarHeight() - Offsets.tabBarHeight()
return CGFloat(screenHeightExceptStatusAndNavAndTabBar / 3)
}
static func navigationAndStatusBarHeight() -> CGFloat{
return Offsets.statusBarHeight() + Offsets.navBarHeight()
}
static func tabBarHeight() -> CGFloat {
return UITabBarController().tabBar.frame.size.height
}
static func navBarHeight() -> CGFloat {
return UINavigationController().navigationBar.frame.size.height
}
static func statusBarHeight() -> CGFloat {
return UIApplication.shared.statusBarFrame.size.height
}
}
打印时,标签栏高度为 49,但是,看起来标签栏顶部到设备底部的距离更大,有屏幕。
如何测量这个距离?
最佳答案
您可以使用 UIApplication 的
shared 中
实例。keyWindow
的 safeAreaInsets
计算屏幕的 bottomInset
if #available(iOS 11.0, *) {
let bottom = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom
print(bottom)
}
关于ios - 获取 iPhone 最新型号的底部高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56865758/