Search code examples
iosswiftuibuttonresizesetbackground

Stop UIButton resizing on background image change


I have built a storyboard with a label, 6 UIButtons in a 3 x 2 grid and another single button below the grid. I've put these into stack views and set up constraints which all seems to work.

I've set the background image of each button which is initially just a grey background (will be more of an icon later) and when a user clicks on the button I change the background image to be a green tick.

However, when it renders, the button increases in size so my 3 x 2 grid looks shoddy.

How can I stop the button resizing when I programmatically change the background (using setBackgroundImage function)?


Solution

  • Try giving some fixed width or height, cause in a stack view it always take what it needs. Or you could make sure buttons are equal between them. Click and drag a button to another with alt key and set it to equal width.