【发布时间】:2018-05-21 13:23:51
【问题描述】:
我有一个由 5 个元素组成的 UIStackView。我希望居中的那个比其他的大(如下图所示)。
我如何创建 UIStackView()
stackView.axis = UILayoutConstraintAxis.horizontal
stackView.distribution = UIStackViewDistribution.fillEqually
stackView.alignment = UIStackViewAlignment.bottom
stackView.spacing = 0.0
stackView.addArrangedSubview(supportedServicesView)
stackView.addArrangedSubview(incidentView)
stackView.addArrangedSubview(contactUsView)
stackView.addArrangedSubview(moreView)
stackView.addArrangedSubview(moreView2)
stackView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(stackView)
stackView.anchor(nil, left: self.view.leftAnchor, bottom: self.view.bottomAnchor, right: self.view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 90, rightConstant: 0, widthConstant: 0, heightConstant: 0)
我如何创建自定义 UIViews 子视图位置;
override func updateConstraints() {
logoImage.anchor(self.topAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
label.anchor(self.logoImage.bottomAnchor, left: self.leftAnchor, bottom: nil, right: self.rightAnchor, topConstant: 10, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
super.updateConstraints()
}
编辑: 当我将宽度锚添加到居中视图时,它会获得更高的宽度,但由于高度相同,它看起来并不大。
contactUsView.widthAnchor.constraint(equalToConstant: self.view.frame.width / 5).isActive = true
编辑 2:当我对 UIStackView 内的任何视图进行高度限制时,Stackviews 位置(仅高度)更改为我给 Views 高度锚点的值。
【问题讨论】:
-
你应该给你想要更大的宽度限制。
-
嗯,我该怎么做呢?与 mybiggerview.frame.size = CGSize(width..)?
-
yourBiggerView?.widthAnchor.constraint(equalToConstant: widthYouWant).isActive = true
-
它给出“无法同时满足约束”。将宽度锚添加到视图后出错。
-
我认为您需要将 .fillEqually 更改为 .fillProportionally
标签: ios swift uistackview