痛點(diǎn):
多個視圖從左到右布局(或者上到下),當(dāng)中間某個視圖因為業(yè)務(wù)需要隱藏時脐区,
雖然設(shè)置了視圖hidden為YES,但是視圖自身的間距還在阔逼,其他視圖依賴它的話抢蚀,會額外多出一塊多余的間距,此時需要花額外的精力去調(diào)整這個間距問題攀例,很繁瑣船逮!
解決方案:
利用該Snapkit擴(kuò)展布局,業(yè)務(wù)視圖設(shè)置hidden即可,間距會自動調(diào)整
接口擴(kuò)展
make.left.equalTo(v.snp.right).offset(10).whenHidden(view, hiddenValue: 0, originalValue: 10)
view.snp_hidden(true)
效果展示
使用范例
//視圖1
let v = UIView()
v.backgroundColor = .yellow
view.addSubview(v)
v.snp.makeConstraints { make in
make.left.equalTo(10)
make.top.equalTo(200)
make.width.height.equalTo(100)
}
//視圖2
let v1 = UIView()
v1.backgroundColor = .red
view.addSubview(v1)
v1.snp.makeConstraints { make in
make.left.equalTo(v.snp.right).offset(10).whenHidden(v, hiddenValue: 0, originalValue: 10)
make.top.equalTo(200)
make.width.height.equalTo(100).whenHidden(v1, hiddenValue: 0, originalValue: 100)
}
let v2 = UIView()
v2.backgroundColor = .blue
view.addSubview(v2)
v2.snp.makeConstraints { make in
make.left.equalTo(v1.snp.right).offset(10).whenHidden(v1, hiddenValue: 0, originalValue: 10)
make.top.equalTo(200)
make.width.height.equalTo(100)
}
//業(yè)務(wù)顯隱
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: DispatchWorkItem(block: {
//隱藏
UIView.animate(withDuration: 0.25) {
v1.snp_hidden(true)
self.view.layoutIfNeeded()
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: DispatchWorkItem(block: {
//顯示
UIView.animate(withDuration: 0.25) {
v1.snp_hidden(false)
self.view.layoutIfNeeded()
}
}))
}))