Home > Back-end >  Constraint for StackView in Xcode so the buttons stay always to the center of the view vertically?
Constraint for StackView in Xcode so the buttons stay always to the center of the view vertically?

Time:11-07

So i created a simple UIView. I added two UIButtons. I have added them in a StackView. I have put the Alignment of the stackview to fill in xcode attributes inspector and the distribution to fill equally and the spacing to 50. Then i added a height constraint of 300 to the stackview and i added a custom font as well to the buttons as well as three constraints. One for the stackview bottom as you see at the screenshot, one for the trailing and one for the leading constraint. I haven't put a top constraint yet. When i change to different devices through xcode the buttons are not centered. I added the top constraint but i guess because of the height constraint at the stackview there is an issue and all constraint become red when i change to another device than the initial i created the stackview. If i move for example to Iphone SE the buttons move to the top side i guess because of the bottom constraint... So is there a way to center the stackview with the buttons as i change to different devices? Any help appreciated.

enter image description here

CodePudding user response:

For a stack view to be centered you needs leading, trailing and vertical centering constraints, stack view will have automatic height based on buttons heights and vertical spacing, no need for bottom constraint

enter image description here

  • Related