随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

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

复制代码
    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. 给约束添加标识

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 每个的约束,追加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
}

  

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//约束更新的处理: 删除子视图相关的所有约束
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)
}<br>      <br>  <br>     //业务逻辑
for item in itemViewList{
    item.updateViewStyle(style.itemViewStyle)
}
configItems()

  

posted on   懂事长qingzZ  阅读(2291)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示