vwarg vwarg - 1 month ago 5x
Objective-C Question

"[[UIScreen mainScreen] bounds]" returns null?

When I try to get the bounds of the main screen with

[[UIScreen mainScreen] bounds]
, it returns null. I've tried with
[[UIScreen applicationFrame] bounds]
as well, still returns null.

The strange part is; if I do
NSLog(@"%@", [UIScreen mainScreen]);
it prints out the correct bounds, albeit I have no idea how to parse it, if it's even possible.

Here's the relevant code:

-(void)loadView {
[super loadView];
CGRect pagingFrame = [[UIScreen mainScreen] bounds];
NSLog(@"%@", [[UIScreen mainScreen] bounds]); //prints null for me
NSLog(@"Width of original frame = %d", pagingFrame.size.width); //prints out 0
pagingFrame.origin.x -= pagingXOrigin;
pagingFrame.size.width += pagingWidthAdd;
NSLog(@"Width of adjusted frame = %d", pagingFrame.size.width); //still prints out 0
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.contentSize = CGSizeMake(pagingFrame.size.width * [self imageCount], pagingFrame.size.height);
self.view = pagingScrollView;
//Initialize sets
recycled = [[NSMutableSet alloc] init];
visible = [[NSMutableSet alloc] init];
[self tilePages];

is declared in the header file, as is recycled and visible.

I'm using XCode 3 and iOS SDK 4.1.

I'm sure there's a very simple solution for this, however I'm still very much a rookie at Objective-C, and thus I cannot solve it.



bounds is a struct, not an object. You cannot output it with the %@ format specifier. Likewise, the members of the struct are floats, not integers. If you output them with %d, you are bound to get wrong results.