支持Xcode10和适配iPhone XS Max、iPhone XR的方法

2020-01-21 07:37:58王振洲

由于iPhone X、iPhone XS、iPhone XS Max、iPhone XR这些机型的navigationBar高度以及tabBar高度都一致,所以可以用allFaceIDCapableDevices是否包含当前设备,来判断当前设备是否有“齐刘海”。

示例:


static let faceIDDeviceArray = Device.allFaceIDCapableDevices

static let navigationHeight: CGFloat = {
    if faceIDDeviceArray.contains(currentDevice) {
      return faceIDDeviceNavHeight
    } else {
      return ordinaryDeviceNavHeight
    }
  }()

同时DeviceKit中也提供这样一个方法,运行模拟器的时候调用,也会返回真实的设备名称


/// Get the real device from a device. If the device is a an iPhone8Plus simulator this function returns .iPhone8Plus (the real device).
  /// If the parameter is a real device, this function returns just that passed parameter.
  ///
  /// - parameter device: A device.
  ///
  /// - returns: the underlying device If the `device` is a `simulator`,
  /// otherwise return the `device`.
  public static func realDevice(from device: DeviceKit.Device) -> DeviceKit.Device

示例:


static let currentDevice = Device.realDevice(from: Device())
if currentDevice == .iPhoneX {}
// 取代以下写法
if Device() == .iPhoneX || Device() == .simulator(.iPhoneX) {}

最后别忘了再切两张启动图,因为iPhone XS和尺寸和iPhone X是一样的,所以iPhone XS可以忽略

iPhone XR:828px x 1792px

iPhone XS Max: 1242px x 2688px

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对ASPKU的支持。


注:相关教程知识阅读请移步到IOS开发频道。