2

I have couple of UIButtons embedded in UIStackView. I understand UIStackView takes only the intrinsic size of UIButton when laying it out. But I want to increase height/width of UIButton when inside UIStackView. Without UIStackView, I can easily increase or decrease UIButtons frame size. What is the deal with UIStackView?

Detail: Let's say I gave 3 UIButtons with only UIImages and no text. Button 1 is 80x80, button 2 image is 30x30, button 3 image is 30x30. I would like touch area of button 2 and 3 to be atleast 45x45 without stretching the image. I can simply resize UIButton like this when it's not in UIStackView. But with UIStackView, all button sizes are the size of images (intrinsic size) which i don't know how to fix.

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • Can you share how would you like the way to show the buttons and what you currently have reached? – DionizB Nov 21 '18 at 16:21
  • 1
    You should be able to add height and/or width constraints to the buttons that you place in a stack view. – Duncan C Nov 21 '18 at 16:24
  • 1
    But using autolayout to set width and height of button, why am I getting autolayout errors (red boxes in storyboard), spacing value needs to be updated? – Deepak Sharma Nov 21 '18 at 16:31

2 Answers2

4

If you add the width/height constraints to the buttons stack view will size it subviews based on the constraints and not the intrinsic size of buttons.

LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
  • I get autolayout errors (red boxes in storyboard) when manually specifying width and height of UIButton. Why should that occur? – Deepak Sharma Nov 21 '18 at 16:34
  • Because `UIStackView` tries to add own constraints according to its distribution and alignment. – olejnjak Nov 21 '18 at 16:52
4

For this purpose I usually use the contentEdgeInsets property. If it is not sufficient, you would need to add some autolayout constraints.

olejnjak
  • 1,163
  • 1
  • 9
  • 23