Get safe area inset top and bottom heights

后端 未结 15 1426
长发绾君心
长发绾君心 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:59

    None of the other answers here worked for me, but this did.

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

提交回复
热议问题