0

I have added the following UIStackView to my ViewController. As the views change the values of textOneLabel and textTwoLabel change.

With the following code the initial StackView is centered and the portions filled proportionally. However with subsequent text combinations the bounds of the StackView don't change, leaving the content off center. How can I change the StackView properties so it will adapt to the content and always stay centered?

headerStackView.axis = .horizontal
headerStackView.distribution = .fillProportionally
headerStackView.spacing = 8
headerStackView.layer.borderWidth = 1
headerStackView.layer.borderColor = UIColor.red.cgColor
    
headerStackView.translatesAutoresizingMaskIntoConstraints = false
headerStackView.topAnchor.constraint(equalTo: timerHeader.topAnchor, constant: 4).isActive = true
headerStackView.centerXAnchor.constraint(equalTo: timerHeader.centerXAnchor).isActive = true
    
headerStackView.addArrangedSubview(textOneLabel)
headerStackView.addArrangedSubview(textTwoLabel)
4
  • It's not clear what you're going for... Do you want the space between the labels to remain centered? Or do you want the center of the stack view to remain centered? That is, do you want it to look like the Top or Bottom examples here: i.sstatic.net/3qilC.png Commented Oct 5, 2021 at 13:58
  • Excellent example. I am trying to do your third option, where the stackView is centered in the view but the content on the right is longer than the content on the left. As noted above the initial view displays correctly, but when the words change, the stackView does not adapt to the new content size. Commented Oct 5, 2021 at 16:00
  • Let's try this again... do you want the stackView FRAME to remain centered? Or, do you want the gap BETWEEN labels to remain centered? i.sstatic.net/AEM36.png ... or, do you want something else? If so, show us an image of how you want it to look. Commented Oct 5, 2021 at 16:39
  • The StackView should always be centered in the view even if the content of the left and the content of the right are different length. Commented Oct 5, 2021 at 17:14

1 Answer 1

1

First, forget you ever heard of .fillProportionally...

  • it doesn't do what you think it does
  • you'll encounter very unexpected layout issues if your stack view has spacing greater than Zero
  • if your stack view has no width (neither width anchor nor leading/trailing anchors), .fillProportionally doesn't do anything

So, change your .distribution to .fill.

Add these lines to control what auto-layout does with your labels:

    textOneLabel.setContentHuggingPriority(.required, for: .horizontal)
    textOneLabel.setContentCompressionResistancePriority(.required, for: .horizontal)

    textTwoLabel.setContentHuggingPriority(.required, for: .horizontal)
    textTwoLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
    

Now, your stackView FRAME will remain centered.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you DonMag.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.