开发者

question about UIView's sizeThatFits [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplicate:

iPhone — the input parameter to UIView's sizeThatFits method

Specifically, what's its argument supposed to be? The documentation says that it's the receiver's current size, but a view can always use self.bounds.size, so that doesn't make sense.

Is it supposed to be the available space? In other words, is the par开发者_StackOverflow中文版ent asking the child, "given that there's available space of X x Y points, how big do you want to be?".


I simply believe the doc is wrong when it says "The current size of the receiver". It is only a use case but there are others: for example if you want the view to return its very best size given an arbitrary size you will call this method passing your arbitrary size as the parameter.

Is it supposed to be the available space? In other words, is the parent asking the child, "given that there's available space of X x Y points, how big do you want to be?".

Yes, you've got the idea BUT don't restrict the meaning of the argument to the "available space". It's just an arbitrary size that may or not correspond to an available space. It depends on how you are using the method. However the view is supposed to always return what it considers to be its best size (the size that best fits its subviews) if it has to fit into the size passed as an argument.

Look here, this should answer your question: iPhone -- the input parameter to UIView's sizeThatFits method


Pretty much exactly that. Classes like UIPickerView and UILabel have content that works best at particular sizes, and as such they return those specific sizes rather than the more general bounds size.


Simply sizeThatFit: method has to be overriden with new CGSize, and sizeToFit method calls sizeThatFit for resizing.


sizeThatFits can be used when the superview is laying out its children.

Let's say you have a superview that's 300 pixels wide and you don't know yet how tall it's going to be - the height will be based on the sum of the height of the children. In this case, the superview would pass in a size to sizeToFit that represents the remaining available space, which would be different than the bounds of the superview.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜