gpt4 book ai didi

swift - 水平 Stackview 左对齐项目

转载 作者:搜寻专家 更新时间:2023-11-01 05:32:51 25 4
gpt4 key购买 nike

我正在创建一个水平的 UIStackView 并希望将其中的项目左对齐。这是我的代码:

let nameLabel: UILabel = {
let label=UILabel()
label.text = "First Name:"
label.textColor = UIColor.black
label.font = UIFont.systemFont(ofSize: 20)
label.textAlignment = .left
label.translatesAutoresizingMaskIntoConstraints=false
return label
}()

let nameTextField: UITextField = {
let label=UITextField()
label.placeholder = "Enter Name"
label.textColor = UIColor.darkGray
label.font = UIFont.systemFont(ofSize: 18)
label.textAlignment = .left
label.isUserInteractionEnabled = false
label.translatesAutoresizingMaskIntoConstraints=false
return label
}()

lazy var firstNameView: UIStackView = {
let stackView = UIStackView(arrangedSubviews: [self.nameLabel, self.nameTextField])
stackView.translatesAutoresizingMaskIntoConstraints = false;
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.alignment = .center
stackView.spacing = 10.0
return stackView
}()

下面是我如何将它添加到 View

self.view.addSubview(firstNameView)

firstNameView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true;
firstNameView.topAnchor.constraint(equalTo: idLabel.bottomAnchor).isActive = true;
firstNameView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
firstNameView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true

我还尝试了以下方法:

firstNameView.trailingAnchor.constraint(greaterThanOrEqualTo: self.view.trailingAnchor).isActive = true

PS:此题不同于UIStackview align left因为我在谈论水平堆栈 View

根据此链接Left aligned horizontal stackview and top aligned vertical stackview有没有其他选择,我真的不想设置固定宽度。

最佳答案

将标签包裹在 UIView 中,并将标签固定到这些 View 的左侧。将 View 放入堆栈 View 中,它们将填充堆栈 View 而无需操作标签。

关于swift - 水平 Stackview 左对齐项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52012414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com