Search code examples
iosswiftxcodeuiimageviewembed

How can I embed and align in storyboard?


I'm trying to embed an imageView, a label inside a View. When I come to add the stackView with the View, I keep on getting red lines and cannot embed it.

I basically am trying to line it all up so when I flip the phone it all syncs.

Click me for pic

Click me for pic


Solution

  • First of all, there's no need to create additional UIView instance to wrap all subviews together when you're already using a UIStackView. Try to put both the label and the image view inside the stack view, then provide constraints for the stack view itself.

    To provide the constrains for a stack view you need to tell the auto layout engine first how you want it to be laid out. From the screenshots provided, I assume you'd like to have it centered in the middle of the screen. To achieve that, set the stack view's centerX and centerY constraints equal to centerX and centerY of the view controller's view. The width and height of the stack view should be calculated automatically based on its inner views intrinsic content size.