imzyf / ios-swift-learning-notes

📝 iOS Swift Learning Notes - see Issues
MIT License
0 stars 0 forks source link

safearea 安全区 #92

Open imzyf opened 6 years ago

imzyf commented 6 years ago
if #available(iOS 11.0, *) {
      scrollView.frame = CGRect(x: 0, y: view.safeAreaInsets.top, width: view.frame.width, height: view.frame.height - view.safeAreaInsets.bottom - view.safeAreaInsets.top)
    } else {
      scrollView.frame = CGRect(x: 0, y: topLayoutGuide.length, width: view.frame.width, height: view.frame.height - topLayoutGuide.length - bottomLayoutGuide.length)
    }
imzyf commented 6 years ago

safeAreaInsets in UIView is 0 on an iPhone X

https://stackoverflow.com/questions/47032855/safeareainsets-in-uiview-is-0-on-an-iphone-x

safeAreaInsets has the correct size in layoutSubviews()

If you're using view in a UIViewController, you can also do it in viewDidLayoutSubviews() and it should work as it did for me.

imzyf commented 6 years ago
    static var safeArea: UIEdgeInsets {
        if #available(iOS 11.0, *) {
            return UIApplication.shared.delegate?.window??.safeAreaInsets ?? UIEdgeInsets.zero
        } else {
            return .zero
        }
    }