iPhone -- UIView 的 sizeThatFits 方法的输入参数 [英] iPhone -- the input parameter to UIView's sizeThatFits method

查看:20
本文介绍了iPhone -- UIView 的 sizeThatFits 方法的输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个方法的签名是:

- (CGSize)sizeThatFits:(CGSize)size

我不明白 size 参数的用途.Apple 的文档指出它是收件人."

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?

推荐答案

首先,这个方法是 AppKit 遗留的(不是​​消极的).

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

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

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.

因此任何 UIView 子类都可以实现 -sizeThatFits: 因为它认为合适(双关语),甚至可以随意忽略大小参数.大多数情况下,当我必须实现这个方法时,我会忽略它,因为我可以从视图的内部状态计算它,但在更复杂的场景中,您可能需要使用 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.

这篇关于iPhone -- UIView 的 sizeThatFits 方法的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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