iOS: Detect if the device is iPhone X family (frameless)

前端 未结 9 1012
情话喂你
情话喂你 2021-02-05 04:07

In my app there is some logic for frameless devices (iPhoneX, Xs Xs max, Xr). Currently it works base on the model of the devices, so, I detect the model by DeviceKit framework.

9条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 04:44

    I am doing it like this because the iPadPro has non-zero safeAreaInsets.

    extension UIDevice {
    
        /// Returns 'true' if the device has a notch
        var hasNotch: Bool {
            guard #available(iOS 11.0, *), let window = UIApplication.shared.keyWindow else { return false }
            let orientation = UIApplication.shared.statusBarOrientation
            if orientation.isPortrait {
                return window.safeAreaInsets.top >= 44
            } else {
                return window.safeAreaInsets.left > 0 || window.safeAreaInsets.right > 0
            }
        }
    
    }
    

提交回复
热议问题