【问题标题】:How to setup constraints with SnapKit for UITextField and UILabel?如何使用 SnapKit 为 UITextField 和 UILabel 设置约束?
【发布时间】:2018-11-27 12:57:30
【问题描述】:

我有一个 [[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()
}

【问题讨论】:

    标签: swift autolayout snapkit


    【解决方案1】:

    你需要

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

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

    【讨论】:

    • 你希望它不被压缩的项目你需要提供的要求不低
    【解决方案2】:

    实现下面的演示。

    label 可以通过下面的属性自动增加高度。 (斯威夫特 5)

    label = UILabel()
    label.numberOfLines = 0
    label.lineBreakMode = .byWordWrapping
    

    当你设置时,它可以使超级视图的高度同时同步
    超级视图的高度与标签相同。

    label = UILabel()
        viewContainer.addSubview(label)
        label.backgroundColor = UIColor.white
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.text = "hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day. hello world, today is a new day. Have a nice day."
        self.addSubview(label)
        label.snp.makeConstraints { (make) in
            let superView = viewContainer!
            make.left.equalTo(superView).offset(10)
            make.right.equalTo(superView).offset(-10)
            make.centerY.equalTo(superView)
        }
    
        viewContainer.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.centerX.equalTo(self)
            make.left.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-10)
            make.height.equalTo(label).offset(100)
        }
    

    下载代码:https://github.com/zgpeace/SnapkitDemo/tree/dynamicHeightLabel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多