在 stackview 中隐藏视图仍然保持约束活动
Hiding a view inside stackview still keeps constraints active
这段代码可以复制粘贴到新建的项目中:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = createLabel()
let imageView = createImageView()
let stackView = UIStackView(arrangedSubviews: [imageView, label])
stackView.axis = .vertical
stackView.spacing = 5
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { (_) in
imageView.isHidden = true
}
}
func createLabel() -> UILabel {
let label = UILabel(frame: .zero)
label.text = "Some Text"
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentHuggingPriority(.required, for: .vertical)
label.backgroundColor = .green
return label
}
func createImageView() -> UIImageView {
let imageView = UIImageView()
imageView.backgroundColor = .red
imageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
return imageView
}
}
它是一个UILabel
和一个UIImageView
里面的一个UIStackView
。当我隐藏 UIImageView
时,我看到 UIStackView
正确地适应了 UILabel's
高度。但是,UIStackView
不会自动适应 UILabel
的宽度。
如何使 UIStackView
调整自身大小使其仅显示可见视图/UILabel
?我为 UIImageView
的高度锚定常量设置了一个变量约束,并在隐藏 UIImageView
时将其关闭,但 UILabel
由于某些奇怪的原因消失了。
尝试在 UIStackView
的属性检查器中将 Distribution 更改为 Fill Equally
,并将 Alignment
更改为 Fill
或 center
,如您所愿成为。
添加堆栈视图alignment
This property determines how the stack view lays out its arranged
views perpendicularly to its axis. The default value is
UIStackView.Alignment.fill.
stackView.alignment = .leading
Stackview 将自身调整为仅可见的 UILabel
这段代码可以复制粘贴到新建的项目中:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = createLabel()
let imageView = createImageView()
let stackView = UIStackView(arrangedSubviews: [imageView, label])
stackView.axis = .vertical
stackView.spacing = 5
view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
Timer.scheduledTimer(withTimeInterval: 1, repeats: false) { (_) in
imageView.isHidden = true
}
}
func createLabel() -> UILabel {
let label = UILabel(frame: .zero)
label.text = "Some Text"
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentHuggingPriority(.required, for: .vertical)
label.backgroundColor = .green
return label
}
func createImageView() -> UIImageView {
let imageView = UIImageView()
imageView.backgroundColor = .red
imageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
imageView.heightAnchor.constraint(equalTo: imageView.widthAnchor).isActive = true
return imageView
}
}
它是一个UILabel
和一个UIImageView
里面的一个UIStackView
。当我隐藏 UIImageView
时,我看到 UIStackView
正确地适应了 UILabel's
高度。但是,UIStackView
不会自动适应 UILabel
的宽度。
如何使 UIStackView
调整自身大小使其仅显示可见视图/UILabel
?我为 UIImageView
的高度锚定常量设置了一个变量约束,并在隐藏 UIImageView
时将其关闭,但 UILabel
由于某些奇怪的原因消失了。
尝试在 UIStackView
的属性检查器中将 Distribution 更改为 Fill Equally
,并将 Alignment
更改为 Fill
或 center
,如您所愿成为。
添加堆栈视图alignment
This property determines how the stack view lays out its arranged views perpendicularly to its axis. The default value is UIStackView.Alignment.fill.
stackView.alignment = .leading
Stackview 将自身调整为仅可见的 UILabel