I'm creating part of my application's UI with Swift and the problem I'm facing is I have a UIStackView
with 3 sub views: 2 UILabels and an UIImageView. Here is my first code
let switchview = UISwitch()
let nodelableview = UILabel()
nodelableview.textAlignment = NSTextAlignment.right
nodelableview.numberOfLines = 0
nodelableview.text = nodes[i].type + " " + nodes[i].node_name
let statLabel = UILabel()
statLabel.textAlignment = NSTextAlignment.left
statLabel.text = nodes[i].stat
let stack = UIStackView()
stack.axis = .horizontal
stack.spacing = 16
stack.addArrangedSubview(statLabel)
stack.addArrangedSubview(nodelableview)
stack.addArrangedSubview(switchview)
cell.nodesView.addArrangedSubview(stack)
the problem with this code is that when the nodelabelview
has long text the UIStackView not extending to make space for 2 or more lines. So I set the alignment to .center
and here is the result
There is empty space left but the first UILabel
is using it for nothing. How can I force the second UILabel
to use available spaces?
cell.nodesView.addArrangedSubview(stack)
line actually do? Are you inserting this horizontal stack view inside another stack view of your cell namelynodesView
? Why is that necessary? – Operculum