fja
fja

Reputation: 1839

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.

enter image description here

My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...

Upvotes: 2

Views: 60

Answers (3)

Ayman Ibrahim
Ayman Ibrahim

Reputation: 1399

Here are a fast tutorial in how to achieve that:

1-

enter image description here

2- completion of the first Gif:

enter image description here

Note you can achieve the same output using a UIStackView

Upvotes: 0

Puneet Sharma
Puneet Sharma

Reputation: 9484

  1. Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.

  2. Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.

  3. Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.

  4. Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.

Upvotes: 0

Milan Nosáľ
Milan Nosáľ

Reputation: 19737

I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).

Upvotes: 3

Related Questions