iOS 11 safe area layout guide backwards compatibility

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

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

16条回答
  •  天命终不由人
    2020-12-02 06:21

    When you have a generic ViewController that all your ViewControllers extend, another solution would be to put the items that should be adjusted in an IBOutletCollection and adjust them programmatically in that GenericViewController. Here's my code :

    @IBOutlet var adjustTopSpaceViews: [UIView]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        adjustViews()
        ....
    }
    
    func adjustViews() {
        guard let views = adjustTopSpaceViews,
            ProcessInfo.processInfo.operatingSystemVersion.majorVersion < 11 else {
                return
        }
        let statusBarHeight = UIApplication.shared.statusBarFrame.height
        for subview in views {
            subview.superview?.constraints.filter({ (constraint) -> Bool in
                return constraint.firstAttribute == .top
                    && constraint.secondAttribute == .top
                    && (constraint.firstItem as? UIView == subview || constraint.secondItem as? UIView == subview)
            }).forEach({ (constraint) in
                constraint.constant += (constraint.firstItem as? UIView == subview) ? statusBarHeight : -statusBarHeight
            })
        }
    }
    

提交回复
热议问题