我正在修改UIView高度的约束,包括nameTextField和使用SnapKit的superView inputContainerView。我安排nameTextField的高度等于inputContainerView的高度的三分之一。我的目的是在按下SegmentedControl时删除nameTextField。 当我尝试修改常量时,它工作得很好。 但是当我尝试将乘数高度值从(1/3)修改为0时,有时它会崩溃,有时我写的其他textField(passwordTextField和emailTextField)也会消失。 我正在使用updateContraint来更新一些约束。 我该如何解决这个问题?谢谢
inputContainerView.snp.makeConstraints { (make) in
make.centerX.equalTo(view.snp.centerX)
make.centerY.equalTo(view.snp.centerY)
// constraintAnchor is equal to offset
make.width.equalTo(view.snp.width).offset(-24)
make.height.equalTo(150)
}
// constraint for nameTextField
inputContainerView.addSubview(nameTextField)
//x y width height constraint using Snap Kit
nameTextField.snp.makeConstraints { (make) in
make.left.equalTo(inputContainerView.snp.left).offset(12)
make.top.equalTo(inputContainerView.snp.top)
make.width.equalTo(inputContainerView.snp.width)
make.height.equalTo(inputContainerView.snp.height).multipliedBy(0.333)
}
func handleLoginRegisterChange() {
let title = loginRegisterSegmentedControl.titleForSegment(at: loginRegisterSegmentedControl.selectedSegmentIndex)
loginRegisterButton.setTitle(title, for: .normal)
// change height of inputcontainerview
if loginRegisterSegmentedControl.selectedSegmentIndex == 0 {
inputContainerView.snp.updateConstraints({ (update) in
update.height.equalTo(100)
})
nameTextField.snp.remakeConstraints({ (remake) in
remake.height.equalTo(inputContainerView.snp.height).multipliedBy(0)
})
} else if loginRegisterSegmentedControl.selectedSegmentIndex == 1 {
inputContainerView.snp.updateConstraints({ (update) in
update.height.equalTo(150)
})
nameTextField.snp.remakeConstraints({ (remake) in
remake.height.equalTo(inputContainerView.snp.height).multipliedBy(0.333)
})
}
}
// constraint for nameSeparator
inputContainerView.addSubview(nameSeparator)
//x y width height constraint using Snap Kit
nameSeparator.snp.makeConstraints { (make) in
make.left.equalTo(inputContainerView.snp.left).offset(12)
make.top.equalTo(nameTextField.snp.bottom)
make.right.equalTo(inputContainerView.snp.right).offset(-12)
make.height.equalTo(1)
}
// constraint for emailTextField
inputContainerView.addSubview(emailTextField)
//x y width height constraint using Snap Kit
emailTextField.snp.makeConstraints { (make) in
make.left.equalTo(inputContainerView.snp.left).offset(12)
make.top.equalTo(nameSeparator.snp.bottom)
make.width.equalTo(inputContainerView.snp.width)
make.height.equalTo(inputContainerView.snp.height).multipliedBy(0.333)
}
// constraint for emailSeparator
inputContainerView.addSubview(emailSeparator)
//x y width height constraint using Snap Kit
emailSeparator.snp.makeConstraints { (make) in
make.left.equalTo(inputContainerView.snp.left).offset(12)
make.top.equalTo(emailTextField.snp.bottom)
make.right.equalTo(inputContainerView.snp.right).offset(-12)
make.height.equalTo(1)
}
// constraint for passwordTextField
inputContainerView.addSubview(passwordTextField)
passwordTextField.snp.makeConstraints { (make) in
make.left.equalTo(inputContainerView.snp.left).offset(12)
make.top.equalTo(emailSeparator.snp.bottom)
make.width.equalTo(inputContainerView.snp.width)
make.height.equalTo(inputContainerView.snp.height).multipliedBy(0.333)
}
答案 0 :(得分:7)
remakeConstraints
将删除所有先前安装的约束。因此,您可能正在删除所有顶级,领先等等约束并重新安装高度。您应该使用updateConstraints
或者最初使用所有声明的约束重新制作。