痛点:
多个视图从左到右布局(或者上到下),当中间某个视图因为业务需要隐藏时,
虽然设置了视图hidden为YES,但是视图自身的间距还在,其他视图依赖它的话,会额外多出一块多余的间距,此时需要花额外的精力去调整这个间距问题,很繁琐!
解决方案:
利用该Snapkit扩展布局,业务视图设置hidden即可,间距会自动调整
接口扩展
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)
}
//业务显隐
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()
}
}))
}))