The iOS app adaptation

1. Background introduction: iOS UI drawing often needs to get the height of statusBar, navigationBar, homeBar, tabBar. Sometimes it’s in UIViewController, sometimes it’s in UIView. So we don’t hope that through the self. The navigationController… , self.tabBarController… This high degree of coupling in the way to take. 2. Consider that OC can be defined using macros and Swift can define a global layout Struct. This article takes Swift as an example. 3. Look at the actual height, focusing on the height of iPad’s statusBar (both simulators)

Simulator device Model Device System Version statusBar navigtionBar tabBar homeBar screenWidth screenheight
iphone5s 12.1 20.0 44.0 49.0 0.0 320.0 568.0
iphoneXR 12.1 44.0 44.0 83.0 35.0 414.0 896.0
iphoneX 12.1 44.0 44.0 83.0 35.0 375.0 812.0
iphone12 Mini 14.1 44.0 44.0 83.0 35.0 375.0 812.0
iphone12 PMax 14.1 47.0 44.0 83.0 35.0 428.0 926.0
iphone12 Pro 14.1 47.0 44.0 83.0 35.0 390.0 844.0
IpadP12.9 – I – 4 14.1 24.0 44.0 65.0 15.0 1024.0 1136.0
ipadP11-i-2 14.1 24.0 44.0 65.0 15.0 834.0 1194.0
ipadAir-4 14.1 24.0 44.0 65.0 15.0 820.0 1180.0
ipadAir-3 13.1 24.0 44.0 50.0 15.0 834.0 1112.0
ipad-8th 14.1 20 44.0 50.0 15.0 810.0 1080.0
# # implementation:
  1. Check whether the device is an iPhone or an iPad
import UIKit import CoreTelephony import AuthenticationServices struct AppAndDeviceInfo { #warning("error") /// The emulator is not available. /// - Returns: description static func deviceIsiPhone() -> Bool { var systemInfo = utsname() uname(&systemInfo) let machineMirror = Mirror(reflecting: systemInfo.machine) let identifier = machineMirror.children.reduce("") { identifier, element in guard let value = element.value as? Int8, value ! = 0 else {return Identifier} return identifier + String(UnicodeScalar(UInt8(value))} // Simulator x86-64 return identifier.contains("iPhone") } /**************************************/ { static let screenSize: CGSize = UIScreen.main.bounds.size static let screenWidth: CGFloat = screenSize.width static let screenHeigth: CGFloat = screenSize.height static var statusBarHeight: CGFloat {# if available (iOS 13.0. *) {let statusManager = UIApplication. Shared. Windows. The first? .windowScene? .statusBarManager return statusManager? .statusBarFrame.height ?? 20.0} else {return UIApplication. Shared. StatusBarFrame. Height}} static var navigationBarHeight: CGFloat {return 44.0} static var navigationBarMaxY: CGFloat {return 44 + statusBarHeight; } static var homeBarHeight: CGFloat { var height: CGFloat = 0 if AppAndDeviceInfo. DeviceIsiPhone () {if statusBarHeight > = {44 / / bang screen mobile phone, Have homeBar height = 34} else {height = 0} return height.} if AppAndDeviceInfo deviceIsiPad () {if tabBarHeight > = {51 Height = 15.0}else {height = 0.0}} return height} Static var tabBarHeight: CGFloat {// include homeBar // static var tabBarHeight: CGFloat {// include homeBar // (the reader can change Settings for storing attribute, the assignment in the delegate, and then use) guard let tabVC: UITabBarController = UIApplication. Shared. KeyWindow? .rootViewController as? UITabBarController else { return getTabbarheightByDeviceInfo() } return tabVC.tabBar.frame.size.height } /// method 2. UITabBarController, can be set in the assignment, and then use the static var storedTabBarHeight: CGFloat? /// method 3. For future devices, the judgment may be wrong & iPad on the same device, different iOS versions will have different heights /// - Returns: description static func getTabbarheightByDeviceInfo() -> CGFloat { if AppAndDeviceInfo.deviceIsiPhone() { if StatusBarHeight > 21.0, 0.0} {83} return return else if AppAndDeviceInfo. DeviceIsiPad () {if statusBarHeight > {23.0 }else {return 50.0}}else {return 0.0}}} /* Demo address: https://github.com/gree180160/BarItemHeight.git */Copy the code