AutoLayout:以视图中心为中心

我想用UIImageViewUILabel创build自定义的UIViewUIImageView应该可以自动resize的图像大小和UILabel应该可以自动resize垂直文本内容。

你能帮我创造一些限制吗? 我需要dynamic视图的中心: UIIamgeViewUILabel应该在我的自定义视图的中心。

我通常通过使用某种types的容器视图来实现这一点。

通过这样做,我可以将containerView放在self.view中(或者任何你想要居中的位置),然后在其中限制内容(imageView和label)。

这里是我很快做的一个工作代码示例。

 //create a container view to hold the centered content UIView *containerView = [UIView autoLayoutView]; //create our content UIImageView *imageView = [UIImageView autoLayoutView]; UILabel *label = [UILabel autoLayoutView]; //configure the imageView and label //... //Note: The imageView and label should be able to satisfy their own width and height. //add the content to our container [containerView addSubview:imageView]; [containerView addSubview:label]; //pin the left and right of our content to the container with a flexible constraint [imageView pinAttribute:NSLayoutAttributeLeft toAttribute:NSLayoutAttributeLeft ofItem:containerView withConstant:0.0 relation:NSLayoutRelationGreaterThanOrEqual]; [label pinAttribute:NSLayoutAttributeLeft toAttribute:NSLayoutAttributeLeft ofItem:containerView withConstant:0.0 relation:NSLayoutRelationGreaterThanOrEqual]; [imageView pinAttribute:NSLayoutAttributeRight toAttribute:NSLayoutAttributeRight ofItem:containerView withConstant:0.0 relation:NSLayoutRelationLessThanOrEqual]; [label pinAttribute:NSLayoutAttributeRight toAttribute:NSLayoutAttributeRight ofItem:containerView withConstant:0.0 relation:NSLayoutRelationLessThanOrEqual]; //also center the content horizontally within the container [imageView centerInContainerOnAxis:NSLayoutAttributeCenterX]; [label centerInContainerOnAxis:NSLayoutAttributeCenterX]; //pin the content vertically [imageView pinToSuperviewEdges:JRTViewPinTopEdge inset:0.0]; [imageView pinAttribute:NSLayoutAttributeBottom toAttribute:NSLayoutAttributeTop ofItem:label withConstant:0.0]; [label pinToSuperviewEdges:JRTViewPinBottomEdge inset:0.0]; //add the container to self.view [self.view addSubview:containerView]; //center our container view [containerView centerInContainerOnAxis:NSLayoutAttributeCenterX]; [containerView centerInContainerOnAxis:NSLayoutAttributeCenterY]; 

对于testing,我将图像视图限制为100pt x 50pt,并给它一个红色的背景,标签为50pt x 100pt,绿色背景,contentView有蓝色背景。 这是我得到的:

在这里输入图像说明

为了方便起见,我使用jrturton的UIView-Autolayout类别,但是如果你愿意的话,你可以很容易地将上面的代码转换成使用原始的NSLayoutConstraints。

类别可以在这里find: UIView-Autolayout
文档可以在这里find: Cocoadocs

图像视图将自动倾向于调整其内容。 但是,如果其他限制条件推动或拉高优先级,它将变得更大或更小。 因此,将内容拥抱和压缩阻力的优先级设置得很高。

该标签通常倾向于是一行,并扩展其宽度以修复该行上的所有内容。 你可以设置其preferredMaxLayoutWidth ,使其倾向于以指定宽度包装成多行。 同样,您应该将内容拥抱和压缩阻力优先级设置得较高。

目前尚不清楚您是否希望容器的大小适合内容的大小,或者是否希望容器的中心距离容器边缘有可能变化的距离。 如果您想要标签上方的图片,下方的图片,引导图片或拖放图片,目前还不清楚。

使容器大小本身是相对简单的。 我猜你想知道如何将图像和标签集中在一个更大的视图。 我会将图像和标签embedded到一个紧密包含它们的视图中。 然后,您可以设置约束来将外部容器中的内部容器视图居中。


所以,假设有一个containerView ,一个imageView和一个label 。 我build议添加一个innerContainerView用于保持图像和标签在一起,垂直居中在containerView 。 约束条件如下所示:

 V:|[imageView]-[label]| |-(>=0)-[imageView]-(>=0)-| |-(>=0)-[label]-(>=0)-| [innerContainerView(==0@50)| [NSLayoutConstraint constraintWithItem:innerContainerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:containerView attribute:NSLayoutAttributeCenterY multiplier:1 constant:0] V:|-(>=0)-innerContainerView-(>=0)-| 

因此:内部容器视图垂直拥抱的图像和标签,这是由标准距离分隔。 内部容器至less与图像视图和标签一样宽。 但是,它试图(低优先级)尽可能小。 这使得它横向拥抱更广泛的图像视图和标签。 然后,内容器在外容器内垂直居中。 外部容器必须至less与内部容器一样高(或者相反,如果不能,内部容器会迫使内部容器更短,这将迫使图像视图或标签更短;这将被抵制通过它们的压缩阻力来解决歧义,设置它们的阻力以具有不同的优先权)。

我用最后一个约束0,但你可能想要使用不同的值。

你可能也想重复水平方向的最后两个约束,尽pipe你可能更喜欢不同的东西。 我主要是针对垂直居中的目标。

我用伪代码expression了这些限制,但是你应该可以用IB来设置它们。