自动版式:中心两个视图中心上海华中心 [英] AutoLayout: center two views center to superview's center

查看:103
本文介绍了自动版式:中心两个视图中心上海华中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建自定义的的UIView 的UIImageView 的UILabel
的UIImageView 应该是自动调整大小的图像尺寸和的UILabel 应该是自动垂直于文字内容调整大小。

I want to create custom UIView with UIImageView and UILabel. UIImageView should be automatically resizable to image size and UILabel should be automatically resizable vertically to text content.

你能帮我创造的制约。我需要的动态视图中心: UIIamgeView 的UILabel 应该是我的自定义视图中心

Could you help me to create constraints. I need to center of dynamic views: UIIamgeView and UILabel should be in center of my custom view.

推荐答案

图片视图将自动倾向于其内容调整大小。它将,然而,获得或大或小,如果其它约束推或具有更高的优先级上它拉。因此,设置其内容拥抱和COM pression性的优先级为高。

The image view will automatically tend to resize with its content. It will, however, get larger or smaller if other constraints push or pull on it with higher priority. So, set its content hugging and compression resistance priorities to high.

的标签通常倾向于是一条线,并延长其宽度来解决其所有在该行的内容。您可以设置它的 preferredMaxLayoutWidth 有它倾向于在指定的宽度换到多行。再次,你应该设置内容拥抱和COM pression性的优先级为高。

The label would normally tend to be one line and extend its width to fix all of its content on that line. You can set its preferredMaxLayoutWidth to have it tend to wrap to multiple lines at the specified width. Again, you should set the content hugging and compression resistance priorities to high.

如果您想要容器自身大小以适应内容目前还不清楚,或者如果你想要的内容要与到容器边缘的潜在变量距离的中心。如果您希望图像标签上方,下方,导致它,或尾随它它也并不清楚。

It's not clear if you want the container to size itself to fit the content or if you want the content to be in the center with a potentially variable distance to the container edges. It's also not clear if you want the image above the label, below it, leading it, or trailing it.

使容器大小本身的含量相对比较简单。我猜你想知道如何中心一个更大的视图中的图像和标签。我会嵌入图像和标签成紧密包含它们的视图。然后,您可以设置限制居中在外容器内的容器视图。

Making the container size itself to the content is relatively straightforward. I'm guessing you want to know how to center the image and label within a larger view. I would embed the image and label into a view which closely contains them. Then you can set up constraints to center that inner container view in the outer container.

因此​​,假设有一个 containerView ,一个的ImageView 标签。我建议将用来保持图像和标签的 innerContainerView ,一起,在 containerView 垂直居中。该限制将如下所示:

So, suppose there's a containerView, an imageView, and a label. I propose adding an innerContainerView used to keep the image and label, together, centered vertically in the containerView. The constraints would look like:

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)-|

所以:内部容器视图垂直拥抱图像和标签,其由标准的距离分开。的内部容器是至少一样宽的图像视图和标签。但是,它会尝试(以低优先级)到尽可能小。这使得它水平地搂图像视图和标签的更宽。然后,将内部容器的外容器内垂直居中。外容器必须至少为内部容器一样高(或者,相反地,如果它不能,这将迫使内部容器要短,这将迫使图像视图或标签要短;将抵制其COM pression性;解决歧义,设置它们的阻抗有不同的优先级)

So: the inner container view vertically hugs the image and the label, which are separated by the standard distance. The inner container is at least as wide as the image view and the label. But, it tries (at a low priority) to be as small as possible. That makes it horizontally hug the wider of the image view and the label. Then, the inner container is centered vertically within the outer container. The outer container must be at least as tall as the inner container (or, conversely, if it can't, it will force the inner container to be shorter, which will force the image view or label to be shorter; that will be resisted by their compression resistance; to resolve ambiguity, set their resistances to have different priorities).

我用0表示最后一个限制条件,但是你可能需要使用不同的值。

I used 0 for that last constraint, but you might want to use a different value.

您可能会想重复最后两个约束水平方向上,太,虽然你可能preFER不同的东西在那里。我主要处理有关垂直居中你的目标。

You would probably want to repeat the last two constraints for the horizontal direction, too, although you may prefer something different there. I'm mostly addressing your goal about the vertically centering.

我前pressed在伪code这些限制,但你应该能够建立他们都与IB。

I expressed these constraints in pseudo-code, but you should be able to set them all up with IB.

这篇关于自动版式:中心两个视图中心上海华中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆