J'essaye d'ajouter deux dynamiques UILabels
à un UIStackLabel
. L'objectif est de centrer les deux étiquettes horizontalement au milieu de la vue.Ajouter par programme des vues à un UIStackView
Les deux UILabels
sont:
var nameLabel: UILabel!
var ageLabel: UILabel!
Et le UIStackView
est:
var stackedInfoView: UIStackView!
J'ai essayé de suivre les directives fournies dans ce answer, configuration mon point de vue en tant que tel:
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
Mon problème est que, stackedInfoView
ne s'affichera pas. En outre, lorsque j'imprime frame
, j'obtiens {{0, 0}, {0, 0}}
. Je reçois également un tas de messages d'erreur à propos de Unable to simultaneously satisfy constraints.
Qu'est-ce que je fais de façon incorrecte en faisant mon UIStackView
? Toute orientation est très appréciée.
Merci! Cela a beaucoup aidé – daspianist