【发布时间】:2015-05-27 12:04:44
【问题描述】:
我想将我的 UIImageView 和 UILabel 居中,如下图所示。 我使用了一个容器来包含UIImageView和UILabel,但是容器不适合UIImageView和UILabel的宽度,所以我需要设置容器的宽度。有什么方法可以解决问题而不设置宽度或计算视图的宽度?图片如下:
【问题讨论】:
标签: ios width containers pure-layout
我想将我的 UIImageView 和 UILabel 居中,如下图所示。 我使用了一个容器来包含UIImageView和UILabel,但是容器不适合UIImageView和UILabel的宽度,所以我需要设置容器的宽度。有什么方法可以解决问题而不设置宽度或计算视图的宽度?图片如下:
【问题讨论】:
标签: ios width containers pure-layout
有四种视图在起作用:
视图位于以下层次结构中:
我假设外部视图的宽度和高度来自其他约束。
我从您提供的图像中看到的图像比标签高,请记住,遵循约束可以实现您想要的:
【讨论】:
Align Y axis of label to container view,不应该是X axis吗?
[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];
感谢@abdullah。他清除了我的思绪。我忘了“将标签的右边缘固定到容器视图”,所以 containerView 的 width 变成了 0。
【讨论】:
这是快速版本:
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)
【讨论】: