Alexis
Alexis

Reputation: 16829

In my iOS app, how can I know the device orientation when viewDidLoad is called?

I'm trying this :

- (void)viewDidLoad {

    [super viewDidLoad];

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

        NSLog(@"PORTRAIT");

    } else {

        NSLog(@"LANDSCAPE");
    }
}

but it seems that [[UIDevice currentDevice] orientation] returns 0 so I can't know in which orientation the device is at that moment. How can I know it ?

Upvotes: 0

Views: 99

Answers (2)

user1459524
user1459524

Reputation: 3603

I am using

UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication]statusBarOrientation]

This from:

iOS Developer Library: Responding to Orientation Changes in a Visible View Controller

The window adjusts the bounds of the view controller’s view. This causes the view to layout its subviews, triggering the view controller’s viewWillLayoutSubviews method. When this method runs, you can query the app object’s statusBarOrientation property to determine the current user interface layout.

Upvotes: 1

ask4asif
ask4asif

Reputation: 676

You can check the orientation of device like this

    if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation))
{
NSLog (@"Device is in Portrait Mode");
}
else
{
NSLog (@"Device is in Landscape Mode");
}

Upvotes: 0

Related Questions