Why do we use a CGRect when creating a UIScreen?


#1

CGRect windowFrame = [[UIScreen mainScreen] bounds]; // Why use a CGRect instead of UIWindow?? UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame]; [self setWindow:theWindow];

Im confused about this…


#2

This works just fine instead:

    ...
    UIWindow * theWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    
    self.window = theWindow;
    
    // Define the frame rectangles of the three UI elements
    CGRect tableFrame = CGRectMake(0, 80, theWindow.bounds.size.width, theWindow.bounds.size.height - 100);
    ...

without creating a CGRect.


#3

In the first version (that you posted), we are getting the rectangle that defines the size of the screen - its x,y,width,height. We do this by asking the UIScreen for its bounds rectangle. The bounds rectangle is a CGRect - which is a struct (not an object).

We then create an instance of UIWindow (which /is/ an object - a view object meant to play host to all the things the user gets to see and touch in your application) and initialize it to be the same size of the screen by feeding its initWithFrame: message the CGRect that the UIScreen gave us.

AntonyBurt’s more concise version takes the UIScreen’s bounds rectangle and feeds it right into the UIWindow’s initializer without first capturing it into a local variable.

In both cases, we’re asking the UIScreen for its x,y,width,height and using that information to create a UIWindow instance.