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:
Asked
Active
Viewed 7,793 times
3 Answers
8
There are four view in action:
- Outer or main view
- Container view (contains image and label)
- Image view
- Label view
The views are in following hierarchy:
- Outer view
- Container View
- Image view
- Label view
- Container 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:
- Align X axis of container view to outer view
- Align Y axis of container view to outer view
- Pin top, left and bottom edge of Image view to container view
- Pin right edge of label to container view
- Align Y axis of label to container view.
- Set horizontal distance between image and label view.

Abdullah
- 7,143
- 6
- 25
- 41
-
Comments like this makes it worthwhile to answer on stackoverflow. Keep it up. – Abdullah Nov 08 '15 at 04:33
-
it works! in my case i put a width constraint to Outer view respect its parent, i did because the label text is dynamic and it could overflow – Jonathan García Dec 02 '15 at 14:23
-
`Align Y axis of label to container view`, shouldn't it be `X axis`? – Khurram Shehzad Oct 07 '16 at 05:53
4
[self.button autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
[self.button autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
[self.button autoSetDimension:ALDimensionHeight toSize:46];
[self.button autoAlignAxis:ALAxisVertical toSameAxisOfView:self.contentView];
[self.containerView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[self.containerView autoAlignAxisToSuperviewAxis:ALAxisVertical];
[self.iconImageView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.containerView];
[self.iconImageView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[self.label autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.containerView];
[self.label autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
[self.label autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.iconImageView withOffset:10];
Thanks to @abdullah. He cleared my mind. I forgot to "Pin right edge of label to container view", so the containerView
's width
became 0.

Iulian Onofrei
- 9,188
- 10
- 67
- 113

Kevin Hawk
- 308
- 2
- 10
2
Here's the swift version:
button.autoPinEdge(toSuperviewEdge: .left, withInset: 15)
button.autoPinEdge(toSuperviewEdge: .right, withInset: 15)
button.autoSetDimension(.height, toSize: 46)
button.autoAlignAxis(.vertical, toSameAxisOf: contentView)
containerView.autoAlignAxis(toSuperviewAxis: .horizontal)
containerView.autoAlignAxis(toSuperviewAxis: .vertical)
iconImageView.autoPinEdge(.left, to: .left, of: containerView)
iconImageView.autoAlignAxis(toSuperviewAxis: .horizontal)
label.autoPinEdge(.right, to: .right, of: containerView)
label.autoAlignAxis(toSuperviewAxis: .horizontal)
label.autoPinEdge(.left, to: .right, of: iconImageView, withOffset: 10.0)

Yalcin Ozdemir
- 524
- 3
- 7