KirillC
KirillC

Reputation: 800

TextBox stretches in stack view Xcode

I have 3 stacks inside another stack and text boxes stretch way too much. I tried to add hight constraint to a TextBox but it gets highlighted in red and TextBox doesn't shrink (screenshot attached). Can anybody recommend a solution?

enter image description here

Upvotes: 1

Views: 1872

Answers (1)

rob mayoff
rob mayoff

Reputation: 386018

It looks like you want to lay out a form with three rows. The three rows are not enough content to fill the screen vertically, so you need to decide where you want the excess space to go, and you need to set up your constraints to put the excess space there.

Let's say you want the rows centered vertically. Then you need to constrain the vertical stack view's Y center to the root view's Y center. Don't put any other constraints on the vertical stack view's height or its top or bottom. Leaving it unconstrained lets it size itself to perfectly fit its arranged subviews without stretching or compressing them.

You're also getting uneven horizontal stretching of your labels and views. To fix this, constrain all of the labels to be equal width to each other. Then set the labels' horizontal content hugging priority to 251 and their horizontal content compression resistance priority to 750. Set the text fields' horizontal content hugging priority to 250 (just less than the labels'). This tells the horizontal stack views to stretch the text fields, rather than the labels, to fill the excess horizontal space.

Result:

layout previews

Here's my document outline:

document outline

Upvotes: 1

Related Questions