【问题标题】:Make one view bigger than others inside UIStackView()在 UIStackView() 中使一个视图比其他视图更大
【发布时间】: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


【解决方案1】:

我刚刚在操场上实现了这个示例。

UIStackView 使用固有内容大小来计算如何将其排列的子视图放置在堆栈视图中,同时考虑轴、分布、间距等。

因此,如果您同时添加高度和宽度约束,您应该会看到它正常工作。请参阅下面的输出示例和屏幕截图。

//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport


let stackview = UIStackView(frame: CGRect(x: 0, y: 0, width: 500, height: 150))
stackview.backgroundColor = .white
let colours: [UIColor] = [
    .blue,
    .green,
    .red,
    .yellow,
    .orange
]

for i in 0...4 {

    let view = UIView(frame: CGRect.zero)
    view.backgroundColor = colours[i]
    view.translatesAutoresizingMaskIntoConstraints = false

    if i == 2 {
        view.heightAnchor.constraint(equalToConstant: 130).isActive = true
    } else {
        view.heightAnchor.constraint(equalToConstant: 80).isActive = true
    }
    view.widthAnchor.constraint(equalToConstant: 75)

    stackview.addArrangedSubview(view)
}

stackview.axis  = .horizontal
stackview.distribution = .fillEqually
stackview.alignment = .bottom
stackview.spacing = 0.5

PlaygroundPage.current.liveView = stackview

您可以将此代码直接放入 Playground 并调整间距、分布等设置以获得所需的输出。

【讨论】:

  • 不错。我不相信view.widthAnchor.constraint(equalToConstant: 75) 是必需的,也没有被使用
  • 从外观上看,没有填充选项覆盖它
  • UIStackView 不尊重其 backgroundColor 属性,因为它是非绘图视图。
猜你喜欢
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
相关资源
最近更新 更多