如何以编程方式更改 stackView 内单个 UIView 的高度?
Posted
技术标签:
【中文标题】如何以编程方式更改 stackView 内单个 UIView 的高度?【英文标题】:How can I change the height of a single UIView inside of a stackView progromatically? 【发布时间】:2020-06-26 19:21:39 【问题描述】:我无法弄清楚如何让两个视图具有相等的高度,而第三个视图的高度比其他两个更大。现在,每当我尝试更改其中一个视图的高度时,它对所有其他视图都执行相同的操作。这是我所拥有的......
任何帮助将不胜感激!
var line1 = UIView()
var line2 = UIView()
var lineText = UIView()
var lineStackView = UIStackView()
line1.backgroundColor = .red
line2.backgroundColor = .blue
lineText.backgroundColor = .orange
line1.translatesAutoresizingMaskIntoConstraints = false
line2.translatesAutoresizingMaskIntoConstraints = false
lineText.translatesAutoresizingMaskIntoConstraints = false
line1.heightAnchor.constraint(equalToConstant: 10).isActive = true
line2.heightAnchor.constraint(equalToConstant: 10).isActive = true
lineText.heightAnchor.constraint(equalToConstant: 20).isActive = true
lineStackView.distribution = .fillEqually
lineStackView.axis = .horizontal
lineStackView.spacing = 20
[line1, line2, lineText].forEachlineStackView.addArrangedSubview($0)
view.addSubview(lineStackView)
lineStackView.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0, left: 40.adjusted, bottom: 0, right: 40.adjusted))
【问题讨论】:
【参考方案1】:更改堆栈视图的.alignment
:
lineStackView.alignment = .top
// or
lineStackView.alignment = .center
// or
lineStackView.alignment = .bottom
【讨论】:
以上是关于如何以编程方式更改 stackView 内单个 UIView 的高度?的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式将自定义 UIView 添加到 UIScrollView 之上的 Stackview?
如何以编程方式更改堆栈面板中所有(n个)texblock的字体大小?