OS X Cocoa Auto布局隐藏元素 [英] OS X Cocoa Auto Layout hidden elements

查看:164
本文介绍了OS X Cocoa Auto布局隐藏元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的自动版面在狮子,因为它似乎相当不错。但我找不到有关如何做事情的好信息。例如:

I am trying to use the new Auto Layout in Lion because it seems quite nice. But I can not find good information about how to do things. For example:

我有两个标签:

+----------------+
| +------------+ |
| + label 1    | |
| +------------+ |
|                |
| +------------+ |
| | label 2    | |
| +------------+ |
+----------------+

但是第一个标签并不总是填充内容,有时只有没有内容。我想做的是自动显示标签2,标签1将在哪里有内容。

but the first label gets not always populated with content, sometimes there ist just no content for it. What I would like to do is to automatically show the label 2 where label 1 would be when it would have content.

+----------------+
| +------------+ |
| + label 2    | |
| +------------+ |
|                |
|                |
|                |
|                |
+----------------+

我需要添加什么约束,所以它自动工作与autolayout?我知道我可以只是代码一切,但我有大约30个这样的标签,图像和按钮的不同的样式和形状是所有可选的,我不想添加一堆行代码,当它可以自动工作很好太。

What constrains would I have to add so it works automatically with autolayout? I understand that I could just code everything, but I have about 30 such labels and images and buttons of different styles and shapes which are all optional and I don't want to add hundrets of lines of code when it could work automatically quite nice too.

如果它不工作,那么我将使用一个WebView,并使用HTML和CSS。

If it does not work then I will just use a WebView and do it with HTML and CSS.

推荐答案

我不认为你可以这样做。如果您使标签2的布局基于标签1的距离约束,即使您使标签1在没有内容时自动折叠为零高度,标签2仍然是向下的距离,即:

I don't think you could do it that way. If you made the layout for label 2 be based on a distance constraint from label 1, even if you made label 1 auto-collapse to zero height when it has no content, label 2 is still going to be that distance down, ie in:

+----------------+
| +------------+ |
| + label 1    | |
| +------------+ |
|        ^       |
|        ^       !
| +------------+ |
| | label 2    | |
| +------------+ |
+----------------+

其中^是自动布局距离约束 - 如果标签1知道当字符串为空时如何变为零高度,您仍然会得到:

Where ^ is the autolayout distance constraint - If Label 1 knows how to become zero height when it's string is empty, you're still going to get:

+----------------+
| +------------+ |
|        ^       |
|        ^       !
| +------------+ |
| | label 2    | |
| +------------+ |
+----------------+

可以手动创建您的NSLayoutConstraint。你可以使第二个属性为标签1的高度,使常数为零,然后仔细计算出乘数将使距离成为你想要的基于非零标签高度的倍数。

Maybe it is possible by creating your NSLayoutConstraint manually. You could make the second attribute be the height of label 1, make the constant zero, and then carefully work out what the multiplier would be to make the distance be what you want based on a multiple of the non-zero label height.

但是,完成所有这些工作之后,您现在已经编码了一个自动调整大小的NSLabel子类,而不是通过可视化语言创建一个约束对象,并将NSLayoutConstraint 。

But having done all this, you've now coded an NSLabel subclass that auto-sizes, created a constraint object manually instead of via the visual language, and bent NSLayoutConstraint beyond its will.

如果标签1的字符串是空白的,我想你最好改变标签2的框架!

I think you're better off just changing the frame of label 2 if label 1's string is blank!

这篇关于OS X Cocoa Auto布局隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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