//显示提交按钮 时的约束

    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

 

 

删除约束 操作:可以查看  源码:

https://github.com/jiyongTeam/JYFilterView

1. 给约束添加标识

                // 每个的约束,追加identifier
                let topConstraint =  currentItem.topAnchor.constraint(equalTo: self.itemsContainerView.topAnchor, constant: (itemHeight + itemYSpacing) * CGFloat(i))
                topConstraint.identifier = "JYFilterItemView"
                topConstraint.isActive = true
                let widthConstraint = currentItem.widthAnchor.constraint(equalTo: self.itemsContainerView.widthAnchor, multiplier: itemWidthScale, constant:  -spacingItemGapWidth)
                widthConstraint.identifier = "JYFilterItemView"
                widthConstraint.isActive = true
                let heightConstraint = currentItem.heightAnchor.constraint(equalToConstant: itemHeight)
                heightConstraint.identifier = "JYFilterItemView"
                heightConstraint.isActive = true
                if lastItem == nil{
                    let leftConstraint =
                        currentItem.leftAnchor.constraint(equalTo: self.itemsContainerView.leftAnchor, constant: horizontalMagin)
                    leftConstraint.identifier = "JYFilterItemView"
                    leftConstraint.isActive = true
                }else{
                    let leftConstraint = currentItem.leftAnchor.constraint(equalTo: (lastItem?.rightAnchor)!, constant: itemXSpacing)
                    leftConstraint.identifier = "JYFilterItemView"
                    leftConstraint.isActive = true
                }
                // 最后一行
                if i == rows - 1{
                    let bottomConstraint = currentItem.bottomAnchor.constraint(equalTo: self.itemsContainerView.bottomAnchor, constant: 0)
                    bottomConstraint.identifier = "JYFilterItemView"
                    bottomConstraint.isActive = true
                }

  

约束更新处理, 删除原来的约束

        //约束更新的处理: 删除子视图相关的所有约束
        let associatedConstraints = itemsContainerView.constraints.filter {
            $0.identifier == "JYFilterItemView"
        }
        NSLayoutConstraint.deactivate(associatedConstraints)
        itemsContainerView.removeConstraints(associatedConstraints)
        
        itemViewList.forEach {
            let itemConstraints = $0.constraints.filter {
                $0.identifier == "JYFilterItemView"
            }
            NSLayoutConstraint.deactivate(itemConstraints)
            $0.removeConstraints(itemConstraints)
        }
      
  
     //业务逻辑 for item in itemViewList{ item.updateViewStyle(style.itemViewStyle) } configItems()

  

posted on 2020-02-19 10:09  懂事长qingzZ  阅读(2264)  评论(0编辑  收藏  举报