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

标签 swift autolayout snapkit

我有一个[[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/

相关文章:

ios - 删除从其子项引用的 UIView?

ios - 管理登录 IOS App 的用户

audio - Swift:如何设置一个与其他 session 优雅混合的 Audio Session (又名 Pod)

ios - 标签栏重叠 View 后面

ios - 接受函数的回调作为参数

swift - 动态配置更新失败

ios - 无法使用 Facebook SDK 登录

ios - 动态单元格高度自动布局 IOS

ios - 如何在 Xcode 6 中使用 AutoLayout 创建动态大小的分页水平 UIScrollView

ios - Koloda - 负载上的动画随机播放效果