Question

Why would [UIScreen mainScreen].bounds.size.height always return 1024 no matter what layout mode the iPad app is in?

I am trying to have a custom view dynamically generated based on layout mode and I always get a height of 1024 back?

+ (CGRect)rectForView {
NSLog(@"The height is %f", [UIScreen mainScreen].bounds.size.height );
return CGRectMake( 0.0f, 0.0f, [[UIScreen mainScreen]bounds].size.width, (([UIScreen mainScreen].bounds.size.height ) - 100));
}
Was it helpful?

Solution

Here is what I did to fix it.

+ (CGRect)rectForView {

if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) {

    return CGRectMake( 0.0f, 0.0f, [[UIScreen mainScreen]bounds].size.height, (([UIScreen mainScreen].bounds.size.width ) - 100));

}

NSLog(@"The height is %f", [UIScreen mainScreen].bounds.size.height );
return CGRectMake( 0.0f, 0.0f, [[UIScreen mainScreen]bounds].size.width, (([UIScreen mainScreen].bounds.size.height ) - 100));
}

OTHER TIPS

This is correct functionality for the bounds and I believe it returns the size of the screen in the orientation it was launched. You will need to look at viewController or navigation controller for the appropriate size of the frame which is dependent on the orientation.

It always returns the coordinates in Portrait mode. You have to change it if you have another orientation.

UIScreen *screen = [UIScreen mainScreen];
CGRect screenRect = screen.bounds; // implicitly in Portrait orientation.

if (UIDeviceOrientationIsLandscape(orientation)) {
    CGRect temp;
    temp.size.width = screenRect.size.height;
    temp.size.height = screenRect.size.width;
    screenRect = temp;
}

The above code is directly taken from OLGhostAlertView.m which is a free alert view available online, but it gives you an idea as to how to get the coorect coordinates.

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