gpt4 book ai didi

ios - 在 Swift 3 中使用 SnapKit 以编程方式修改约束以删除一个 UIView

转载 作者:行者123 更新时间:2023-11-28 09:35:35 27 4
gpt4 key购买 nike

我正在使用 SnapKit 修改 UIView 高度的约束,包括 nameTextField 及其 superView inputContainerView。我将 nameTextField 的高度设置为 inputContainerView 高度的三分之一。我的目的是在按下 SegmentedControl 时删除 nameTextField。当我尝试修改常量时,它工作得很好。但是,当我尝试将乘数高度值从 (1/3) 修改为 0 时,有时它会崩溃,有时我编写的其他文本字段(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)
}

图片如下: enter image description here enter image description here

最佳答案

remakeConstraints 将删除所有以前安装的约束。因此,您可能会删除所有顶部、前导等约束,并仅重新安装高度。您应该使用 updateConstraints 或重新制作最初声明的所有约束。

关于ios - 在 Swift 3 中使用 SnapKit 以编程方式修改约束以删除一个 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46021600/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com