5

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 parent asking the child, "given that there's available space of X x Y points, how big do you want to be?".

Community
  • 1
  • 1
Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55

4 Answers4

8

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

Community
  • 1
  • 1
thomas.g
  • 3,894
  • 3
  • 29
  • 36
2

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.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • Perhaps I wasn't clear enough. I meant to ask: why does the function need to take an argument? If the view wants to know its current size, it can use self.bounds. – Kartick Vaddadi May 01 '11 at 05:13
0

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

kinghomer
  • 3,021
  • 2
  • 33
  • 56
  • You haven't answered my question: why does sizeThatFits need to take the current size of the view as an argument, when it can just read self.bounds.size? – Kartick Vaddadi Nov 14 '11 at 02:12
-1

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.

shim
  • 9,289
  • 12
  • 69
  • 108
stevex
  • 5,589
  • 37
  • 52
  • That contradicts the documentation I quoted in my question -- the argument is supposed to be the child's bounds, but the child already knows its bounds. – Kartick Vaddadi Dec 13 '11 at 02:58