【发布时间】:2017-10-09 18:52:45
【问题描述】:
我正在尝试将两个动态 UILabels 添加到 UIStackLabel。目标是将两个标签水平居中在视图的中间。
这两个UILabels是:
var nameLabel: UILabel!
var ageLabel: UILabel!
UIStackView 是:
var stackedInfoView: UIStackView!
我已尝试遵循此answer 中提供的指南,将我的视图配置为:
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
我的问题是,stackedInfoView 不会出现。此外,当我打印它的frame 时,我得到{{0, 0}, {0, 0}}。我还收到一堆关于Unable to simultaneously satisfy constraints. 的错误消息
我在创建UIStackView 时做错了什么?非常感谢任何指导。
【问题讨论】:
标签: ios swift autolayout uistackview