这应该做到:
var p: Float = 1000
for v in stackView.arrangedSubviews {
v.setContentCompressionResistancePriority(UILayoutPriority(rawValue: p), for: .vertical)
p -= 1
}
不过,使用堆栈视图有点奇怪,它可能最终不能满足您的需要。
例如,使用
100 x 150
框架和14个标签,我们得到:
正如我们所见,底部的标签有点“关闭”
而且,只有5个标签,我们得到:
顶部标签的高度会被拉长,除非我们用内容优先权做同样的事情。。。在这种情况下,底部标签会被拉伸。
更好的方法是将堆栈视图嵌入
UIView
UIView公司
.clipsToBounds = true
有14个标签:
下面是要玩的代码。。。
方法1:
class FrankMethod1ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let stackView = UIStackView()
stackView.alignment = .leading
stackView.axis = .vertical
view.addSubview(stackView)
stackView.frame = CGRect(x: 100, y: 100, width: 100, height: 150)
// change to (0..<5) to see the stretching
(0..<15).forEach {
let label = UILabel()
label.text = "text \($0)"
label.backgroundColor = .red
stackView.addArrangedSubview(label)
}
var p: Float = 1000
for v in stackView.arrangedSubviews {
v.setContentCompressionResistancePriority(UILayoutPriority(rawValue: p), for: .vertical)
p -= 1
}
}
}
class FrankMethod2ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let cView = UIView()
cView.frame = CGRect(x: 100, y: 100, width: 100, height: 150)
cView.clipsToBounds = true
// so we can see the view frame
cView.backgroundColor = .cyan
view.addSubview(cView)
let stackView = UIStackView()
stackView.alignment = .leading
stackView.axis = .vertical
cView.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: cView.topAnchor),
stackView.leadingAnchor.constraint(equalTo: cView.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: cView.trailingAnchor),
])
// change to (0..<5) to see NO stretching
(0..<15).forEach {
let label = UILabel()
label.text = "text \($0)"
label.backgroundColor = .red
stackView.addArrangedSubview(label)
}
}
}
编辑
我得到的是:
import UIKit
import PlaygroundSupport
class PlaygroundFrankViewController: UIViewController {
override func loadView() {
let view = UIView()
view.backgroundColor = .white
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
let stackView1 = UIStackView()
stackView1.alignment = .leading
stackView1.axis = .vertical
view.addSubview(stackView1)
stackView1.frame = CGRect(x: 50, y: 50, width: 100, height: 150)
// change to (0..<5) to see the stretching
(0..<15).forEach {
let label = UILabel()
label.text = "text \($0)"
label.backgroundColor = .red
stackView1.addArrangedSubview(label)
}
var p: Float = 1000
for v in stackView1.arrangedSubviews {
v.setContentCompressionResistancePriority(UILayoutPriority(rawValue: p), for: .vertical)
p -= 1
}
let stackView2 = UIStackView()
stackView2.alignment = .leading
stackView2.axis = .vertical
view.addSubview(stackView2)
stackView2.frame = CGRect(x: 200, y: 50, width: 100, height: 150)
// change to (0..<5) to see the stretching
(0..<5).forEach {
let label = UILabel()
label.text = "text \($0)"
label.backgroundColor = .red
stackView2.addArrangedSubview(label)
}
p = 1000
for v in stackView2.arrangedSubviews {
v.setContentCompressionResistancePriority(UILayoutPriority(rawValue: p), for: .vertical)
p -= 1
}
let cView1 = UIView()
cView1.frame = CGRect(x: 50, y: 220, width: 100, height: 150)
cView1.clipsToBounds = true
// so we can see the view frame
cView1.backgroundColor = .cyan
view.addSubview(cView1)
let stackView3 = UIStackView()
stackView3.alignment = .leading
stackView3.axis = .vertical
cView1.addSubview(stackView3)
stackView3.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView3.topAnchor.constraint(equalTo: cView1.topAnchor),
stackView3.leadingAnchor.constraint(equalTo: cView1.leadingAnchor),
stackView3.trailingAnchor.constraint(equalTo: cView1.trailingAnchor),
])
// change to (0..<5) to see NO stretching
(0..<15).forEach {
let label = UILabel()
label.text = "text \($0)"
label.backgroundColor = .red
stackView3.addArrangedSubview(label)
}
let cView2 = UIView()
cView2.frame = CGRect(x: 200, y: 220, width: 100, height: 150)
cView2.clipsToBounds = true
// so we can see the view frame
cView2.backgroundColor = .cyan
view.addSubview(cView2)
let stackView4 = UIStackView()
stackView4.alignment = .leading
stackView4.axis = .vertical
cView2.addSubview(stackView4)
stackView4.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView4.topAnchor.constraint(equalTo: cView2.topAnchor),
stackView4.leadingAnchor.constraint(equalTo: cView2.leadingAnchor),
stackView4.trailingAnchor.constraint(equalTo: cView2.trailingAnchor),
])
// change to (0..<5) to see NO stretching
(0..<5).forEach {
let label = UILabel()
label.text = "text \($0)"
label.backgroundColor = .red
stackView4.addArrangedSubview(label)
}
}
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = PlaygroundFrankViewController()