0

Let's examine this line of codes (only 1 actually change anything)

PO(self.containerForFormerHeader);
PO(self.containerForFormerHeader.subviews);
[self.containerForFormerHeader sizeToFit];
PO(self.containerForFormerHeader);

Result:

self.containerForFormerHeader: <UIView: 0x8b669c0; frame = (0 75; 320 0); autoresize = W+H; layer = <CALayer: 0x8b5eb30>>
self.containerForFormerHeader.subviews: (
    "<UIImageView: 0xd572210; frame = (0 0; 320 10); autoresize = LM+RM+TM; userInteractionEnabled = NO; layer = <CALayer: 0xd572250>> - shading-top-Table.png"
)
self.containerForFormerHeader: <UIView: 0x8b669c0; frame = (0 75; 320 0); autoresize = W+H; layer = <CALayer: 0x8b5eb30>>

From the result it's obvious that:

  1. The view has a height of 0.
  2. The view has a subview whose height is 10
  3. After sizetofit, nothing change. How come?

I can compute the frame directly however, this bothers me.

Anonymous White
  • 2,149
  • 3
  • 20
  • 27
  • can you explain more as to what you are doing & maybe post some code? – Srikar Appalaraju Oct 24 '12 at 01:04
  • If the subview and the superview are the exact same size, what is the point of having a superview (besides making the graphics hardware work a little harder to comply with your request to layer an extraneous view onto the window)? – CodaFi Oct 24 '12 at 02:12

1 Answers1

3

When you call the sizeToFit method on a view, it ends up calling the sizeThatFits: method on the view. The default implementation of sizeThatFits: returns the view's current size.

So unless your custom view explicitly overrides the sizeThatFits: method to return an appropriate size to contain its subviews, nothing will change size when you call sizeToFit.

This is all spelled out in the docs for UIView sizeToFit and UIView sizeThatFits:.

Classes like UILabel do implement sizeThatFits: to return an appropriate size.

rmaddy
  • 314,917
  • 42
  • 532
  • 579