Init vs initWithFrame in a view?


In Chapter 5 we were very careful to initWithFrame the HypnosisView so that it was drawing in a particular frame.

In Chapter 6, we set the view of the ViewController to a HypnosisView that simply has init called. We never explicitly call initWithFrame, although I did a little experimentation (added an NSLog to initWithFrame in HypnosisView) and it turns out it is implicitly called. The setRootViewController doesn’t call it, so I was confused as to when it is called.

Being extra-curious, I set a breakpoint inside of HypnosisView initWithFrame and discovered that UIView is calling it when we do HypnosisView alloc.init. This is perplexing to me, because it implies that UIView is doing work for us that we did manually in Chapter 5. Is this simply a shortcut that would have worked in Chapter 5? Does UIView send window.bounds as the frame by default and we needed to override it for the later examples and so did it in the first example too? (Clearly window.bounds wouldn’t work when we wanted a double-sized frame, or a frame that was offscreen in the UIScrollView example.)

Anyway, if folks can explain this, I’d appreciate it!


Hi sulamsulam,

I have been bothered by the same question and I did some research.
So it looks like BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] init]; call is equal to BNRHypnosisView *backgroundView = [[BNRHypnosisView alloc] initWithFrame:CGRectZero];
As you can guess, CGRectZero is equivalent to CGRectMake(0,0,0,0).

Hope that helps!