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

前端 未结 9 862
情话喂你
情话喂你 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:26

    This way you can cover all the orientations:

    var hasTopNotch: Bool 
    {
        if #available(iOS 11.0,  *) {
    
            var safeAreaInset: CGFloat?
            if (UIApplication.shared.statusBarOrientation == .portrait) {
                safeAreaInset = UIApplication.shared.delegate?.window??.safeAreaInsets.top
            }
            else if (UIApplication.shared.statusBarOrientation == .landscapeLeft) {
                safeAreaInset = UIApplication.shared.delegate?.window??.safeAreaInsets.left
            }
            else if (UIApplication.shared.statusBarOrientation == .landscapeRight) {
                safeAreaInset = UIApplication.shared.delegate?.window??.safeAreaInsets.right
            }
            return safeAreaInset ?? 0 > 24
        }
        return false
    }
    

提交回复
热议问题