I'm modifying the constraint of UIView height consist of nameTextField and its superView inputContainerView using SnapKit. I arrange the height of nameTextField is equal to one-third of the inputContainerView's height. My purpose is to remove the nameTextField when SegmentedControl is pressed. When I try to modify the constant, it worked perfectly. But when I try to modify the multiplier height value from (1/3) to 0, sometimes it crashed and sometimes the other textField that I wrote (passwordTextField and emailTextField) is vanished. I'm using updateContraint to update some of the constraint. How am I supposed to fix this? Thank you
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)
}
Here's the image are shown:
remakeConstraints
will remove all previously installed constraints. So, you are probably removing all of the top, leading, and etc contraints and reinstalling just the height. You should use updateConstraints
or do a remake with all of the declared constraints initially.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With