Search code examples
iosinterface-builderconstraintsstackview

How to use Stack View to center a button in a container?


I am new to Stack Views so bear with me. I have an outer Stack View that contains all my elements as displayed in the image:

enter image description here

My problem is that the "Skip" button is now outside its container. I have tried to set constraints for "Center Horizontally" and "Center Vertically", but this causes my image to look like this:

enter image description here

As we can see, both my button and my container are no longer appearing in the Scene. What am I doing wrong here?

Here are my constraints for my container: enter image description here

Here are my constraints for my "Skip" button: enter image description here


Solution

  • Stack views essentially work on the principle of stacking objects which will obviously remove the prior constraints. The previous answer is correct but does not take into consideration stack views as asked by the question.

    The simplest fix would to essentially "group" your button and image by placing them in a view and then adding the view into the stack view.