-1

我正在开发一个可以在屏幕上显示 10 个项目的快速应用程序。如果我想在不会改变大小的屏幕上执行此操作,即用户不改变方向或 iPad 用户不使用分屏,我将能够通过let size = bounds.width/19.

问题在于屏幕尺寸是动态的,因此我需要在约束条件下进行。我不想使用 UICollectionView ,因为它太重了,如果可能的话也不喜欢使用 UIStackView ,因为我认为它不支持我需要的圆形纵横比。我正在尝试使用 UIViews。

编辑: 这就是我希望他们看起来的样子。这将是大约 50 高,其他信息将在下面。

在此处输入图像描述

4

1 回答 1

2

UIStackView是这项工作的正确工具。将来,我建议更严格地先定义您想要发生的事情,然后再深入研究文档。

let sv = UIStackView()
sv.spacing = 10
// this means each arranged subview will take up the same amount of space
sv.distribution = .fillEqually 

for _ in 0..<50 { 
    // omitting the rounded corners or any other styling because
    // it's not the point of this question
    let subview = UIView()
    // The stack view will determine the width based on the screen size
    // we just need to say that height == width
    subview.widthAnchor.constraint(equalTo: subview.heightAnchor).isActive = true
    sv.addArrangedSubview(subview)
}

// add your stackview to your view hierarchy and constrain it
于 2018-08-30T17:24:44.750 回答