The Window

The top of the view hierarchy is the app’s window. It is an instance of UIWindow (or your own subclass thereof), which is a UIView subclass. Your app should have exactly one main window. It occupies the entire screen and forms the background to, and is the ultimate superview of, all your other visible views. Other views are visible by virtue of being subviews, at some depth, of your app’s window. (If your app can display views on an external screen, you’ll create an additional UIWindow to contain those views; but in this chapter I’ll behave as if there were just one screen, the device’s own screen, and just one window.)

The Xcode project templates all generate your app’s window for you. The technique used, in a nonstoryboard app, is to create the window explicitly in code, in the app delegate’s application:didFinishLaunchingWithOptions:. The window must persist for the lifetime of the app, so the app delegate has a window property that retains it:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

The window’s designated initializer is initWithFrame:; I’ll explain in a moment what “frame” and “bounds” are, but the effect is to make the window the same size as the screen. In the template, the comment, “Override point for customization after application launch,” comes after that line of code, because any code customizing what’s in the window will need the window to exist first. The various templates adopt various strategies for giving the window ...

Get Programming iOS 6, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.