ios - UIStackView 内的 UITextField 宽度约束问题

标签 ios swift uitextfield uistackview

我有一个垂直的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)>"

)

Will attempt to recover by breaking constraint

如何声明 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

但这也会导致中断错误。

编辑2:此堆栈 View 位于自定义 UIView 中,我在内部提供 anchor

 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)
}

我想在图片中实现什么:

Image

最佳答案

如果您已经设置了 stackView.distribution = .fillEqually,则只需添加一个高度约束即可向任何文本文件添加一个约束,如下所示

let height = NSLayoutConstraint(item: textField1, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 40.0)
textField1.addConstraint = height

通过这种方式,所有文本字段的高度将为 40,总大小将取决于项目的数量和给定的间距,即 Height = 40Space = 10 那么总高度将为(高度 * 文本字段数量) + (空格 * (文本字段数量 - 1))

NOTE : Concept is same for vertical and horizontal stackview you need to play with height and width.

关于ios - UIStackView 内的 UITextField 宽度约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944572/

相关文章:

ios - 正确配置闭包以捕获结果

ios - 如何使用 NSDateFormatter 创建计时器(计时)?

ios - PubNub使用,Swift 2.0更新 "Ambiguous use of '下标'"+ "排序》

iOS 9 StackView inside ScrollView 不填满屏幕宽度

ios - 媒体查询和 Bootstrap 模式?

ios - 选中行时防止 TableView 滚动

ios - 如何在用户在 Swift 中键入时调整 UITextField 的大小

ios - 更改焦点上的文本字段

swift - 当用户在 UItextfields 中快速输入时如何验证和更新模型

ios - UICollectionView 并使用 Parse 推送到 detailViewController