gpt4 book ai didi

swift - 如何使用 SnapKit 为 UITextField 和 UILabel 设置约束?

转载 作者:行者123 更新时间:2023-12-02 20:11:32 32 4
gpt4 key购买 nike

我有一个[[TextField需要最大可用宽度----]offset(10.0)][Label]]。我想将 TextFeild 引脚设置为左侧并缩小所有可用空间,而无需修剪标签,并将标签设置为右侧引脚并获得最小的适合尺寸。

lazy var textField: UITextField = {
var textField = UITextField()
textField.placeholder = "Placeholder"
textField.delegate = self
textField.borderStyle = UITextField.BorderStyle.none
textField.keyboardType = UIKeyboardType.numberPad
textField.returnKeyType = UIReturnKeyType.done
textField.setContentHuggingPriority(.defaultHigh, for: .horizontal)

return textField
}()

lazy var measureLabel: UILabel = {
var label = UILabel()
label.numberOfLines = 1
label.setContentHuggingPriority(.defaultLow, for: .horizontal)
label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

return label
}()

measureLabel.snp.makeConstraints { (make) in
make.right.equalTo(self.snp.right)
make.centerY.equalToSuperview()
}
textField.snp.makeConstraints { (make) in
make.left.equalToSuperview()
make.right.equalTo(self.measureLabel.snp.left).offset(-10.0)
make.centerY.equalToSuperview()
}

最佳答案

你需要

label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentCompressionResistancePriority(.required, for: .horizontal)

您也可以完全删除这两行,因为默认情况下文本字段的 ContentHuggingPriority && ContentCompressionResistancePriority 低于 label 的默认值,而且文本字段没有固有大小

关于swift - 如何使用 SnapKit 为 UITextField 和 UILabel 设置约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500265/

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