-1

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.

Stack View

4
  • 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? Commented Dec 1, 2023 at 0:52
  • My labelview on the right is expanding, and my two texts are both crammed to the right...
    – Rik
    Commented Dec 1, 2023 at 1:10
  • Can you provide enough code to reproduce your problem?
    – Elevo
    Commented 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.
    – HangarRash
    Commented Dec 1, 2023 at 5:44

3 Answers 3

1

Most straightforward way...

Give your stack view these properties:

enter image description here

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:

enter image description here

1

You can't do it directly. However, there are a few tricks to archive this layout.

  1. Add two labels, aligning them one to the left and one to the right.

enter image description here

  1. Add two labels and a space between them.

enter image description here

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.

1

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)
    ])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.