iPhone——UIView的sizeThatFits方法的输入参数

发布于 2024-09-29 03:42:20 字数 356 浏览 2 评论 0原文

这个方法的签名是:

- (CGSize)sizeThatFits:(CGSize)size

我不明白size参数是用来做什么的。 Apple 的文档指出,“当前的大小接收器。”

但接收者大概知道其当前的大小。那么为什么需要传入呢?

当我实验性地传递其他值时,该方法似乎无论如何都使用接收器的当前大小。

谁能解释一下吗?在任何情况下这个参数都很重要吗?

The signature of this method is:

- (CGSize)sizeThatFits:(CGSize)size

I don't understand what the size parameter is used for. Apple's documentation states that it is "The current size of the receiver."

But the receiver presumably knows its current size. So why does it need to be passed in?

When I experimentally pass in other values, the method appears to use the receiver's current size anyway.

Can anyone explain? And is there any case where this parameter matters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别挽留 2024-10-06 03:42:20

首先,这个方法是AppKit遗留的(不是​​这个词的负面意义)。

是的,任何视图在任何给定时刻都有一定的当前大小,并且可以从bounds属性中检索它。但在布局过程中,当最佳尺寸取决于不完全静态的因素时,会出现一些棘手的情况。以文本标签为例。它可以排成一行或多行,并且行数可能取决于最大允许宽度。因此,可能的 UILabel 实现可以从传递给 sizeThatFits: 的 CGSize 宽度得出其边界大小,在这种情况下,该大小实际上并不是接收器的当前大小,而是一些所需/限制大小。

因此,任何 UIView 子类都可以实现 -sizeThatFits: 因为它认为合适(双关语),甚至可以自由地忽略大小参数。大多数情况下,当我必须实现此方法时,我会忽略它,因为我可以根据视图的内部状态来计算它,但在更复杂的情况下,您可能需要使用 size 参数来提示自己布局中的某些限制。

First of all, this method is AppKit legacy (not in the negative sense of the word).

Yes, any view has some current size at any given moment and can retrieve it from the bounds property. But there are tricky situations during layout when the best size depends on not-quite-static factors. Take a text label, for example. It may be flowed in one or more lines and the number of lines may depend on the maximum allowed width. So a possible UILabel implementation could derive its bounds size from the width of the CGSize passed to sizeThatFits:, in which case that size is not literally the current size of the receiver, but some desired/limit size.

Thus any UIView subclass can implement -sizeThatFits: as it sees fit (pun intended), and is even free to ignore the size parameter. Most often when I have to implement this method, I ignore it because I can calculate it from the internal state of the view, but in a more complex scenario you might need to use the size parameter to hint yourself about certain restrictions in layout.

如梦 2024-10-06 03:42:20

这不仅仅是接收器的尺寸,也是您想要填充的潜在尺寸。结果是视图认为能够最好地显示给定输入大小的内容的大小。

默认行为是简单地返回 size 参数(即适合默认视图的大小是您指定的大小) - 所以是的,默认情况下此参数很重要。

子类可以使用此方法强制执行诸如 width==height 之类的约束或其他类似的事情。

It is not just the size of the receiver is is the potential size size you want to fill. The result is that size that the view believes will best show its contents for the given input size.

The default behavior is to simply return the size parameter (i.e. the size that fits the default view is the size you give it)- so yes, this parameter matters by default.

Subclasses could enforce constraints like width==height or other things like that using this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文