How to get iPhones current orientation?
Is there a special method to get iPhones orientation? I don't need it in degrees or radians, I want it to return an UIInterfaceOrientation object. I just need it for an if-else construction like
if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}
if (curren开发者_运维百科tOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}
Thanks in advance!
This is most likely what you want:
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
You can then use system macros like:
if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
{
}
If you want the device orientation use:
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
This includes enumerations like UIDeviceOrientationFaceUp
and UIDeviceOrientationFaceDown
As discussed in other answers, you need the interfaceOrientation, not the deviceOrientation.
The easiest way to get to this, is to use the property interfaceOrientation on your UIViewController. (So most often, just: self.interfaceOrientation will do).
Possible values are:
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
Remember: Left orientation is entered by turning your device to the right.
Here is a snippet of code I hade to write because I was getting wierd issues coming back into my root view when the orientation was changed .... I you see just call out the method that should have been called but did seem to be .... this works great no errors
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
//do something or rather
[self
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
NSLog(@"landscape left");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
//do something or rather
[self
shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
NSLog(@"landscape right");
}
if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
//do something or rather
[self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
NSLog(@"portrait");
}
}
UIInterfaceOrientation
is now deprecated, and UIDeviceOrientation
includes UIDeviceOrientationFaceUp
and UIDeviceOrientationFaceDown
so can't be relied on to give you the interface orientation.
The solution is pretty simple though
if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
// Landscape
} else {
// Portrait
}
With [UIApplication statusBarOrientation]
being deprecated you should now use:
UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];
UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;
精彩评论