iOS how to center two view via PureLayout

Kevin Hawk picture Kevin Hawk · Mar 24, 2015 · Viewed 7.2k times · Source

I want to center my UIImageView and UILabel like the pic below. I used a container to contain UIImageView and UILabel,but the container does not suit the width of UIImageView and UILabel.So I need to set the width of the container.Is there any method to solve the problem without set the width or calculate the view's width? Here's the picture:

MyLabel

Answer

Abdullah picture Abdullah · Mar 24, 2015

There are four view in action:

  1. Outer or main view
  2. Container view (contains image and label)
  3. Image view
  4. Label view

The views are in following hierarchy:

  1. Outer view
    1. Container View
      1. Image view
      2. Label view

I assume that the outer view gets its width and height from other constraints.

What I see from the image you provided that image is taller than label, keeping that in mind following constrains could achieve what you want:

  1. Align X axis of container view to outer view
  2. Align Y axis of container view to outer view
  3. Pin top, left and bottom edge of Image view to container view
  4. Pin right edge of label to container view
  5. Align Y axis of label to container view.
  6. Set horizontal distance between image and label view.