Mrk Fldig
Mrk Fldig

Reputation: 4486

Resizing Button within a stackview

So I've be studying Swift/iOS development recently and I'm having a little trouble with sizing buttons within a stack view, I have the following layout:

enter image description here

There are two TextFields and a button in a vertical StackView, the StackView is centred horizontally and vertically(with a bit of spacing). It's anchored width ways to the left and right margin.

I wanted to reduce the size of the button so it's about 80% of the width of the StackView.

Things I've tried:

I'm doing all this in the storyboard view rather than in code as I was trying to get to know the interface.

I've had a good hunt through and can't seem to find any answers that apply, apologies if it seems basic but thought it might be beneficial on the off chance anybody else new to Xcode/Swift comes across this.

Upvotes: 20

Views: 30350

Answers (3)

Bilal
Bilal

Reputation: 19156

Try this..

  1. Remove leading and trailing constraints of the button.
  2. Add constraint Horizontal in Container to the button.
  3. Add constraint Equal Width of the button with super view (which should be stack view I suppose). Like this.. Press control + drag the pointer from button the StackView and then selec Equal widths from the small popup menu.

  4. Edit the Equal Width constraint and set Multiplier to 0.8.

enter image description here enter image description here

Upvotes: 11

Fabio Almeida
Fabio Almeida

Reputation: 31

Hold the Control button and click and drag the button you want to resize to the same button and select Width and Height so select Add constrains. This will give you a size of image button, so you can change this in Inspector. Remember to change view to Scale to Fill. This is solved for me.

Upvotes: 2

MLBDG
MLBDG

Reputation: 1367

For those trying to resize the height of some view inside a stackView like in the following example:

enter image description here

First, set the constraints for the StackView(width/height,x,y), then set the StackView properties as follows:

enter image description here

Next, select the view you want to resize. I.e. the green one, and add these constraints:

enter image description here

Then just add more constraints to the other views/button/labels whatever. In this example here are the constraints for the blue and orange views:

Blue: Constraints of the blue view

And,

Orange:

Constraints of the orange view

Hope it helps.

Upvotes: 14

Related Questions