tiamat
tiamat

Reputation: 971

Swift right padding / trailing not taken into account

I would like to get my StackView centered on the screen and also add a padding on each sides (left & right) of 20 to get margin with the screen:

enter image description here

but the left margin is not taken into account: enter image description here

result is the following:

enter image description here

how can I handle the right margin ? I also had the same problem for other type of objects (not a StackView).

Upvotes: 1

Views: 234

Answers (1)

dcompiled
dcompiled

Reputation: 181

make sure you are only using the minimum number of constraints needed to establish the frame for your view. For example:

  • Leading constraint & width constraint
  • Trailing constraint & width constraint
  • Center X constraint & width constraint
  • Leading constraint & trailing constraint

Incorrect examples would be:

  • Center X constraint & trailing constraint (doesn't specify where the leading edge should go)
  • Center X constraint & leading constraint & trailing constraint (more constraints than necessary, plus unless the leading and trailing constraints are equal, they will be in conflict with the centering

The other thing I'm seeing is that you don't have any constraints for top / bottom / height (the vertical axis) so Interface Builder is warning you that you don't have enough constraints to correctly position the view. Make sure that you don't have any red errors or red constraints in Interface Builder, otherwise the layout is undefined or ambiguous.

Upvotes: 1

Related Questions