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

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

问题描述

此方法的签名是:

   - (CGSize)sizeThatFits:(CGSize)size 

我不明白size参数的用途。 Apple的文档声明它是当前的大小收件人。



但接收器可能知道它的当前大小。那为什么需要传递呢?



当我通过实验传递其他值时,该方法似乎仍然使用接收器的当前大小。



可以谁解释一下?有没有这个参数重要的情况?

解决方案

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



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



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

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?

解决方案

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.

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

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