Diego Petrucci Diego Petrucci - 1 year ago 57
Swift Question

Reduce space around a textfield using auto layout

I'm currently using a stackView that contains a few vertically aligned elements. I'm doing everything in code.

I've marked every stack view's subview. Here's a screenshot:

enter image description here

This is the code for the stackview:

func setUpVerticalLayoutStackView() {
stackViewForVerticalLayout = UIStackView(arrangedSubviews: [viewTitleButton, stackViewForTheTwoPersons, owedButton, amountTextField, doneButton])
stackViewForVerticalLayout.axis = .vertical
stackViewForVerticalLayout.distribution = .equalCentering
stackViewForVerticalLayout.alignment = .center
stackViewForVerticalLayout.spacing = 20
stackViewForVerticalLayout.translatesAutoresizingMaskIntoConstraints = false

My question is: why does the textfield occupy so much space? Can I reduce it somehow?

Don't mind the "r" at the center of the view, it's just something I mistakenly added in an image editor.

Wes Wes
Answer Source

Change stackViewForVerticalLayout.distribution to .equalSpacing or .fillEqually depending on what you want. This will reduce this size of the field