2

Im making a custom keyboard, and wanting a label and a textField centered in the space left over the keyboard, I figured the proper way would be to incorporate them in a UIstackView, and then centering the stack. However, i'm having som issues with the stack resizing my textField, causing whatever text i enter to be clipped.

I tried adding compressionresistance and a number of different solutions, but its clear to me that im missing some information about how UIStackViews work. Usually i make them work, but the whole resizing part I dont understand.

The first two screens are without a stack, adding them as subview to the viewcontrollers view, and the second screenshot shows the stackView resizing my textfield everytime the keyboard is resigned.

How can i stop the stackview from resizing its contained views?

enter image description here enter image description here

enter image description here

enter image description here

Lord Fresh
  • 636
  • 8
  • 15

2 Answers2

2

It seems UIStackView is inherently a auto layout component, and when it adds your arranged subviews, it automatically positions them using constraints.

Since the UIStackView is adding constraints of its own, and the best way I found to stop it, was to simply explicitly set the subviews constraits:

textField.widthAnchor.constraint(equalToConstant: 300).isActive = true
Lord Fresh
  • 636
  • 8
  • 15
0

Apple doesnt like dynamic StackViews: "In general, you should not use dynamic stack views to simply implement a scratch-built table view clone." Read the Apple Documentation on UIStackView for details.

Karsten
  • 1,869
  • 22
  • 38