Is it possible to have a stack view with two labels in it, but instead of being trailing or leading aligned, the labels are aligned to the edges? Thanks.
-
From docs: "The stack view aligns the first and last arranged view with its edges along the stack’s axis. In a horizontal stack, this means the first arranged view’s leading edge is pinned to the stack’s leading edge, and the last arranged view’s trailing edge is pinned to the stack’s trailing edge." So I don't understand how what you want is different from default behavior. Can you provide an example of how it arranges views not the way you want and what is not right there?– timbre timbreCommented Dec 1, 2023 at 0:52
-
My labelview on the right is expanding, and my two texts are both crammed to the right...– RikCommented Dec 1, 2023 at 1:10
-
Can you provide enough code to reproduce your problem?– ElevoCommented Dec 1, 2023 at 2:09
-
I've read this question several times and I still can't figure out what your goal is. Please update your question and make it clear what result you are trying to achieve. You should also show what you have done so far so people can help you update your code.– HangarRashCommented Dec 1, 2023 at 5:44
3 Answers
Most straightforward way...
Give your stack view these properties:
Constrain the stack view Top/Leading/Trailing and desired Height constraints.
Give the Left Label desired Width and Height constraints.
Constrain Right Label equal Width and Height to Left Label.
Looks like this:
You can't do it directly. However, there are a few tricks to archive this layout.
- Add two labels, aligning them one to the left and one to the right.
- Add two labels and a space between them.
Notes: Be careful when setting content hugging priority
for these views. i.e. if you want the left one always show up fully (without trimming "..."), increase its content hugging priority horizontal.
It is indeed possible to have a stack view with two labels aligned to the edges instead of being trailing or leading aligned. In this scenario, you can use the UIStackView with horizontal distribution set to fill and alignment set to fill. This configuration ensures that the labels stretch to fill the available space horizontally and align to the edges of the stack view.
// Create two labels
let firstLabel = UILabel()
firstLabel.text = "Hello Team"
let secondLabel = UILabel()
secondLabel.text = "Doing Well!"
// Create a stack view
let stackView = UIStackView(arrangedSubviews: [firstLabel, secondLabel])
// Set stack view properties
stackView.axis = .horizontal
stackView.distribution = .fill
stackView.alignment = .fill
// Add stack view to the view hierarchy
view.addSubview(stackView)
// Add constraints to the stack view
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView.topAnchor.constraint(equalTo: view.topAnchor),
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])