7

I'm starting with iOS app and creating a table view. In my table I have a cell like this.

enter image description here

Now I need to center the horizontal stackview with stars vertically and horizontally. This stackview has fixed width and height.

It have to look like this.

enter image description here

For this I put it in another horizontal stackview. I tried to add the same constraints to it as you see for the label

The problem is that this external stackview does not stretch like a label. I also see that it's width and height are greyed out, I cannot change them. Why?

enter image description here

Vitalii
  • 10,091
  • 18
  • 83
  • 151
  • Can't you just center horizontally the StackView ? –  Mar 20 '18 at 16:02
  • Please post a screenshot of the Attributes Inspector for that Stack View – Andy Obusek Mar 20 '18 at 16:03
  • 1
    Don't put your "stars" stack view inside another stack view. Remove the Leading and Trailing constraints, and simply constrain it "centered horizontally" to the label above it. – DonMag Mar 20 '18 at 16:31

1 Answers1

6

There are many ways to autolayout this.

I think you should gain a better understanding of StackViews for future usage. I recommend reading on these resources:

In order to achieve what you need:

  1. Use only 1 horizontal StackView containing the stars. Constraint the stackview to the label by center horizontally.

  2. Use 2 StackViews and keep your constraints. Outer stackview, change it to vertical axis and center alignment.

Method 2 Image in Attributes Inspector (cmd + option + 4):

Attributes Inspector1

pkamb
  • 33,281
  • 23
  • 160
  • 191
Errol
  • 278
  • 2
  • 7
  • 1
    "Method 2" *is* an option, but using a stack view to align one element is really the wrong way to go about it. – DonMag Mar 20 '18 at 16:42
  • There are merits to both layouts. Option 1 is dependent on the label's layout. If a constraint on the label breaks (though unlikely) it'll also mess up the layout of the stars. Stackviews are cheap as it's only a transformation layer. – Errol Mar 21 '18 at 16:10