iOS 11 safe area layout guide backwards compatibility

前端 未结 16 1158
忘了有多久
忘了有多久 2020-12-02 05:42

Is enabling Safe Area Layout Guides compatible to iOS below 11?

16条回答
  •  生来不讨喜
    2020-12-02 05:56

    Swift 5

    I just do this. It is simple and very close to the real thing (just added an 'r').

    extension UIView {
        var saferAreaLayoutGuide: UILayoutGuide {
            get {
                if #available(iOS 11.0, *) {
                    return self.safeAreaLayoutGuide
                } else {
                    return self.layoutMarginsGuide
                }
            }
        }
    }
    

    Use like this:

    button.topAnchor.constraint(equalTo: view.saferAreaLayoutGuide.topAnchor, constant: 16)
    

提交回复
热议问题