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

前端 未结 9 843
情话喂你
情话喂你 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
    }
    
    0 讨论(0)
  • 2021-02-05 04:32
    extension UIDevice {
        var hasNotch: Bool {
            let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
            return bottom > 0
        }
    }
    

    use 'UIDevice.current.hasNotch' will return true or false

    Swift 5

    var hasNotch: Bool {
        let bottom = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
        return bottom > 0
    }
    
    0 讨论(0)
  • 2021-02-05 04:33
    extension UIDevice {
        var hasNotch: Bool
        {
            if #available(iOS 11.0, *)
            {
                let bottom = UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0
                return bottom > 0
            } else
            {
                // Fallback on earlier versions
                return false
            }
        }
    }
    

    Using by

    if UIDevice.current.hasNotch 
    {
        //... consider notch
    } 
    else 
    {
       //... don't have to consider notch
    }
    
    0 讨论(0)
  • 2021-02-05 04:35

    Since keyWindow was deprecated in iOS 13, based on the solution to find keyWindow from here, this one works for me

    extension UIDevice {
        var hasNotch: Bool {
            if #available(iOS 11.0, *) {
                let keyWindow = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
                return keyWindow?.safeAreaInsets.bottom ?? 0 > 0
            }
            return false
        }
    
    }
    
    0 讨论(0)
  • 2021-02-05 04:41

    This is valid for any orientation. No need to worry about iOS version before 11.0 since iPhone X minimum version is 11.0. Source

    extension UIDevice {
    
        var hasNotch: Bool {
            if #available(iOS 11.0, *) {
               return UIApplication.shared.keyWindow?.safeAreaInsets.bottom ?? 0 > 0
            }
            return false
       }
    }
    
    0 讨论(0)
  • 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
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题