Ios 获取iPhone最新型号底部高度
我有一个函数,假设计算屏幕高度的1/3。以下是实施方案:Ios 获取iPhone最新型号底部高度,ios,swift,Ios,Swift,我有一个函数,假设计算屏幕高度的1/3。以下是实施方案: struct Offsets { static let singlePixel = (1.0 / UIScreen.main.scale) static func thirdScreenHeightExceptTabAndNavBar() -> CGFloat { let screenHeight = UIScreen.main.bounds.size.height let screenHeightExc
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,但看起来标签栏顶部到设备底部的距离更大,屏幕上没有显示
如何测量此距离?您可以获得如下所示的偏移。新槽口使用safeAreaLayout显示,因此您可以根据它进行计算
if #available(iOS 11.0, *) {
let bottom = view.safeAreaInsets.bottom
}
您可以使用
ui应用程序的共享实例中keyWindow
的safeAreaInsets
来计算屏幕的bottomInset
if #available(iOS 11.0, *) {
let bottom = UIApplication.shared.keyWindow?.rootViewController?.view.safeAreaInsets.bottom
print(bottom)
}
我的类没有视图的引用。请尝试从窗口获取俯视图。窗口可从UIC应用程序访问