Question about initWithFrame:CGRectZero


On page 360, the loadView method for TouchViewController.m looks like this:

- (void)loadView { [self setView:[[TouchDrawView alloc] initWithFrame:CGRectZero]]; }

I have a question about the CGRectZero. It seems like our TouchDrawView expands to fit the entire dimensions of the enclosing view, which I take to be the application’s window. How does that happen? Looking at the documentation, I’m guessing that when we set the TouchViewController instance as the root view controller for the window (in AppDelegate), that the window sends the view a sizeThatFits: message. Is this what happens? Otherwise, I don’t understand how the view expands after we’ve set its dimensions to zero.



Maybe, default autoresizing mask at work here?


I second this question. The documentation for CGRectZero is that it is a x = 0, y = 0, width = 0, height = 0 rect… how does it get to expand to take over the whole window? Is this something to do with being the view of the root view controller?


I’m pretty certain that my first guess is wrong and that sizeThatFits: doesn’t do what I thought it does. I re-read the documentation for the method, after overriding the method in TouchDrawView.m like this:

- (CGSize)sizeThatFits:(CGSize)size { NSLog(@"width %f, height %f", size.width, size.height); return [super sizeThatFits:size]; }

The method doesn’t even get called, so I’m still not sure how the view knows to size itself to the dimensions of its parent view. Anyone able to make sense of this?


This apple page hints at the window setting the frame of the rootViewController to its frame, but its not very clear. Look at the second heading.


Ok… and here it is:

From the “View Management” section of the UIViewController Class Reference:

( … rence.html )



(I’m looking for the “Like” button, but can’t find it.) :wink: