Get safe area inset top and bottom heights

后端 未结 15 1417
长发绾君心
长发绾君心 2020-11-30 17:30

On the new iPhone X, what would be the most proper way to get both top and bottom height for the unsafe areas?

15条回答
  •  一向
    一向 (楼主)
    2020-11-30 17:38

    Swift 5 Extension

    This can be used as a Extension and called with: UIApplication.topSafeAreaHeight

    extension UIApplication {
        static var topSafeAreaHeight: CGFloat {
            var topSafeAreaHeight: CGFloat = 0
             if #available(iOS 11.0, *) {
                   let window = UIApplication.shared.windows[0]
                   let safeFrame = window.safeAreaLayoutGuide.layoutFrame
                   topSafeAreaHeight = safeFrame.minY
                 }
            return topSafeAreaHeight
        }
    }
    

    Extension of UIApplication is optional, can be an extension of UIView or whatever is preferred, or probably even better a global function.

提交回复
热议问题