我有一个垂直的UIStackView
,里面有各种各样的UITextFields
(现在它只有4个)。当我给UIStackView留出间距时,我给出了以下错误;
"<NSLayoutConstraint:0x1c4286720 H:|-(0)-[UIStackView:0x104d3e6f0](LTR) (active, names: '|':Hello.OtpInputView:0x104d3c5b0 )>", "<NSLayoutConstraint:0x1c4286770 UIStackView:0x104d3e6f0.right == Hello.OtpInputView:0x104d3c5b0.right (active)>", "<NSLayoutConstraint:0x1c4286860 '_UITemporaryLayoutWidth' Hello.OtpInputView:0x104d3c5b0.width == 0 (active)>", "<NSLayoutConstraint:0x1c4286e50 'UISV-canvas-connection' UIStackView:0x104d3e6f0.leading == Hello.CustomOtpTextField:0x10588d600.leading (active)>", "<NSLayoutConstraint:0x1c4286ea0 'UISV-canvas-connection' H:[Hello.CustomOtpTextField:0x105082600]-(0)-| (active, names: '|':UIStackView:0x104d3e6f0 )>", "<NSLayoutConstraint:0x1c4286f40 'UISV-fill-equally' Hello.CustomOtpTextField:0x105024600.width == Hello.CustomOtpTextField:0x10588d600.width (active)>", "<NSLayoutConstraint:0x1c4287030 'UISV-fill-equally' Hello.CustomOtpTextField:0x105077a00.width == Hello.CustomOtpTextField:0x10588d600.width (active)>", "<NSLayoutConstraint:0x1c42871c0 'UISV-fill-equally' Hello.CustomOtpTextField:0x105082600.width == Hello.CustomOtpTextField:0x10588d600.width (active)>", "<NSLayoutConstraint:0x1c4286ef0 'UISV-spacing' H:[Hello.CustomOtpTextField:0x10588d600]-(10)-[Hello.CustomOtpTextField:0x105024600] (active)>", "<NSLayoutConstraint:0x1c4286fe0 'UISV-spacing' H:[Hello.CustomOtpTextField:0x105024600]-(10)-[Hello.CustomOtpTextField:0x105077a00] (active)>", "<NSLayoutConstraint:0x1c42870d0 'UISV-spacing' H:[Hello.CustomOtpTextField:0x105077a00]-(10)-[Hello.CustomOtpTextField:0x105082600] (active)>"
)
将尝试通过打破约束来恢复
我如何声明StackView:
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.axis = .horizontal
stackView.distribution = .fillEqually
stackView.spacing = 10
//textFields is just an array of UITextFields
textFields.enumerated().forEach { (index,field) in
field.textColor = UIColor.darkGray
field.font = Font.sourceSansRegular?.withSize(46)
field.keyboardType = .numberPad
field.delegate = self
field.translatesAutoresizingMaskIntoConstraints = false
field.tag = index
field.textAlignment = .center
field.myDelegate = self
field.sizeToFit()
stackView.addArrangedSubview(field)
field.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: (CGFloat(0 / textFields.count)),constant: CGFloat(10 * (textFields.count - 1))).isActive = true
}
self.addSubview(stackView)
stackView.anchor(self.topAnchor, left: self.leftAnchor, bottom: self.bottomAnchor, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
我想要的是例如UIStackView
有110pt宽度和10空间然后UITextFields
应该有20pt((20 * 4)+(3 * 10),这是110)。因此,所有UITextFields
都应具有相同的宽度(同等填充)
我怎样才能做到这一点?
编辑:我想我需要为一个TextField指定至少一个宽度,所以我添加了
field.widthAnchor.constraint(equalTo: stackView.widthAnchor, multiplier: (CGFloat(0 / textFields.count)),constant: CGFloat(10 * (textFields.count - 1))).isActive = true
但是这也会给出中断错误。
EDIT2:这个堆栈视图在一个自定义的UIView中,我在里面给锚点
override init(frame: CGRect) {
super.init(frame: frame)
//Where I gave all anchors
setupUI()
}
另外我如何初始化这个自定义UIView
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor.white
self.view.addSubview(otpField)
otpField.anchor(self.view.topAnchor, left: self.view.leftAnchor, bottom: nil, right: self.view.rightAnchor, topConstant: 40, leftConstant: 40, bottomConstant: 0, rightConstant: 40, widthConstant: 0, heightConstant: 60)
}
我想在图片中实现的目标:
如果你已经设置了stackView.distribution = .fillEqually
为你的任何textFiled添加一个约束,你只需要为高度添加一个约束
let height = NSLayoutConstraint(item: textField1, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 40.0)
textField1.addConstraint = height
通过这样做你的所有文本字段将是高度40,总大小将取决于项目的数量和间隔给予,即Height = 40
,Space = 10
然后总高度将是(Height * Number of textfields) + (Space * (number of textfield - 1))
注意:垂直和水平堆栈视图的概念是相同的,您需要使用高度和宽度。