Question

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];
}

pagingScrollView 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.

Thanks

Was it helpful?

Solution

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.

OTHER TIPS

The NSStringFromCGRect method makes it much easier to quickly log a CGRect. See Apple's documentation for more info, but here's a code snippet:

CGRect screenBounds = [[UIScreen mainScreen] bounds];
NSLog(@"%@", NSStringFromCGRect(screenBounds));

To further explain a little, "%@" formats NSObjects by calling [obj description] on the object and using the string returned from that. So before you try to format a string with %@ make sure that you're actually dealing with an NSObject and not some other type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top