我似乎无法形成约束的工作方式,因此在描述性术语中:
我可以或多或少地为大多数人定义约束,除非屏幕在宽度缩小以适应更小的屏幕尺寸时自动约束其宽度。
一些指针会很好
谢谢!
答案 0 :(得分:2)
这应该这样做:
let textField = UITextField()
view.addSubview(textField)
textField.translatesAutoresizingMaskIntoConstraints = false
// center horizontally
textField.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
// 100 from top layout guide
textField.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor, constant: 100).isActive = true
// make sure textfield width never exceeds the superview width
textField.leftAnchor.constraint(greaterThanOrEqualTo: view.leftAnchor).isActive = true
textField.rightAnchor.constraint(lessThanOrEqualTo: view.rightAnchor).isActive = true
// give the textfield its default width, notice the lowered priority which allows this constraint to be broken when needed to satisfy the above constraints
let widthConstraint = textField.widthAnchor.constraint(equalToConstant: 370)
widthConstraint.priority = UILayoutPriorityDefaultHigh
widthConstraint.isActive = true
答案 1 :(得分:2)
您可以向具有较低优先级的textField添加前导和尾随约束。只需添加宽度约束即可<= 370。