简体   繁体   English

以编程方式将视图添加到UIStackView

[英]Programmatically adding views to a UIStackView

I am attempting to add two dynamic UILabels to a UIStackLabel . 我试图两个动态添加UILabelsUIStackLabel The goal is center the two labels horizontally in the middle of the view. 目标是将两个标签水平放置在视图中间。

The two UILabels are: 两个UILabels是:

var nameLabel: UILabel!
var ageLabel: UILabel!

And the UIStackView is: UIStackView是:

var stackedInfoView: UIStackView!

I have attempted to follow the guidelines provided in this answer , configuring my views as such: 我试图遵循本回答中提供的指南,配置我的观点:

nameLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 120.0, height: 24.0))
ageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 80.0, height: 24.0))
nameLabel.text = "Harry Potter"
ageLabel.text = "100"


stackedInfoView = UIStackView(arrangedSubviews: [nameLabel, ageLabel])
stackedInfoView.axis = .horizontal
stackedInfoView.distribution = .equalSpacing
stackedInfoView.alignment = .center
stackedInfoView.spacing = 30.0
stackedInfoView.centerXAnchor.constraint(equalTo: self.extendedNavView.centerXAnchor).isActive = true
stackedInfoView.centerYAnchor.constraint(equalTo: self.extendedNavView.centerYAnchor).isActive = true

self.extendedNavView.addSubview(stackedInfoView) //extendedNavView is configured inside Storyboard

My issue is that, stackedInfoView will not show up. 我的问题是, stackedInfoView不会出现。 Furthermore, when I print its frame , I get {{0, 0}, {0, 0}} . 此外,当我打印它的frame ,我得到{{0, 0}, {0, 0}} I also get a bunch of error messages about Unable to simultaneously satisfy constraints. 我还得到一堆关于Unable to simultaneously satisfy constraints.的错误消息Unable to simultaneously satisfy constraints.

What am I doing incorrectly in making my UIStackView ? 我在制作UIStackView什么? Any guidance is much appreciated. 任何指导都非常感谢。

Two problems, 两个问题,

stackedInfoView.centerXAnchor.constraint(equalTo:self.extendedNavView.centerXAnchor).isActive = true
stackedInfoView.centerYAnchor.constraint(equalTo:self.extendedNavView.centerYAnchor).isActive = true

should be put after self.extendedNavView.addSubview(stackedInfoView) . 应该放在self.extendedNavView.addSubview(stackedInfoView) Because the stack view must be in the view hierarchy before configure the constraints. 因为在配置约束之前,堆栈视图必须位于视图层次结构中。

Second, add stackedInfoView.translatesAutoresizingMaskIntoConstraints = false to tell UIKit you want to use auto layout to set the position of the stack view. 其次,添加stackedInfoView.translatesAutoresizingMaskIntoConstraints = false告诉UIKit您要使用自动布局来设置堆栈视图的位置。

The following code should work if extendedNavView has no layout issue in the storyboard: 如果extendedNavView在故事板中没有布局问题,则以下代码应该有效:

override func viewDidLoad() {
    super.viewDidLoad()

    var nameLabel: UILabel!
    var ageLabel: UILabel!

    var stackedInfoView: UIStackView!

    nameLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 120.0, height: 24.0))
    ageLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 80.0, height: 24.0))
    nameLabel.text = "Harry Potter"
    ageLabel.text = "100"

    stackedInfoView = UIStackView(arrangedSubviews: [nameLabel, ageLabel])
    stackedInfoView.axis = .horizontal
    stackedInfoView.distribution = .equalSpacing
    stackedInfoView.alignment = .center
    stackedInfoView.spacing = 30.0

    stackedInfoView.translatesAutoresizingMaskIntoConstraints = false

    self.extendedNavView.addSubview(stackedInfoView) //extendedNavView is configured inside Storyboard

    stackedInfoView.centerXAnchor.constraint(equalTo: self.extendedNavView.centerXAnchor).isActive = true
    stackedInfoView.centerYAnchor.constraint(equalTo: self.extendedNavView.centerYAnchor).isActive = true

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM