Tim Bernikovich
Tim Bernikovich

Reputation: 5945

How to get full screen bounds with correct orientation?

guys. I have such problem: I need to use full screen for my view. So I use

[_activityIndicator setFrame:[[UIScreen mainScreen] applicationFrame]];

It's work perfect, but only in portrait view. What to do if my app have to work in landscape too?

Upvotes: 0

Views: 4038

Answers (2)

matt
matt

Reputation: 534925

Your app has a root view controller, which rotates in response to device rotation. So set your frame in terms of the root view controller's view. Use its bounds, not its frame.

You can get a reference to the root view controller through the main window. You can get a reference to the main window through the shared application:

UIWindow* theWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController* rvc = theWindow.rootViewController;
UIView* mainView = rvc.view;
UIActivityIndicatorView* act = 
    [[UIActivityIndicatorView alloc] initWithFrame: mainView.bounds];
[mainView addSubview: act];

And then you retain a reference to the activity indicator in a property and set it spinning.

Upvotes: 2

B.S.
B.S.

Reputation: 21726

To get current screen bounds i use:

CGRect screenBoundsDependOnOrientation()
{
    CGRect screenBounds = [UIScreen mainScreen].bounds ;
    CGFloat width = CGRectGetWidth(screenBounds)  ;
    CGFloat height = CGRectGetHeight(screenBounds) ;
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        screenBounds.size = CGSizeMake(width, height);
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        screenBounds.size = CGSizeMake(height, width);
    }
    return screenBounds ;
}

Upvotes: 1

Related Questions