简体   繁体   中英

Screen Orientation for Portrait and Landscape Orientation in iOS (Objective-C)

I have a situation where I want to call separate method for different Orientation (Portrait and Landscape).

For example:

If (Orientation == Portrait)
{
  Some method a;
}

Elseif (Orientation == Landscape)
{
  Some method b;
}

I use [[UIApplication sharedApplication] statusBarOrientation] to know the orientation.

then I use this method

if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Do something if landscape
} else {
//Do something in portrait
}

don't use [[UIDevice currentDevice] orientation] because it doesn't work properly if the device is on a table, for example.

    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
    {
         // code for landscape orientation      
    }

    if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
    {
         // code for Portrait orientation       
    }

UIDeviceOrientationIsLandscape and UIDeviceOrientationIsPortrait are the macro's.

UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
if(deviceOrientation == UIDeviceOrientationPortrait)
    ...
else  ...

the enum is UIDeviceOrientation[something]

TRY THIS:

UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)

    {// perform ua operation for landscape mode

} else{

//perform ua operation for portrait mode };

Objective-c:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    if (UIDevice.currentDevice.orientation == UIInterfaceOrientationPortrait) {
        printf("Portrait");
    } else {
        printf("Landscape");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM