NSLayoutConstraint 设置优先级

时间:2022-06-24 21:54:33

 

//显示提交按钮 时的约束
    private var svBottomCon:NSLayoutConstraint?
    
    //隐藏提交按钮 时的约束
    private var svUpdateBottomCon:NSLayoutConstraint?


    //显示有数据的UI , 不能编辑,隐藏提交按钮  此时设至 高约束等级 defaultHigh
  func showDataUI(){ bindCardView.updateTempUI() self.submitBtn.isHidden = true if #available(iOS 11.0, *) { svUpdateBottomCon = scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20) } else { svUpdateBottomCon = scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20) } svUpdateBottomCon?.priority = UILayoutPriority.defaultHigh svUpdateBottomCon?.isActive = true } 
//默认UI 设置 第一次的约束等级是
defaultLow
if #available(iOS 11.0, *) { scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true svBottomCon = scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -102) submitBtn.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -30).isActive = true } else { scrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20).isActive = true svBottomCon = scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -102) submitBtn.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30).isActive = true } svBottomCon?.priority = UILayoutPriority.defaultLow svBottomCon?.isActive = true