iOS: Device orientation on load

2019-01-06 10:50发布

It seems that when my app loads, it does not know its current orientation:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
    NSLog(@"portrait");// only works after a rotation, not on loading app
}

Once I rotate the device, I get a correct orientation, but when I load the app, without changing the orientation, it seems that using [[UIDevice currentDevice] orientation] doesn't know the current orientation.

Is there another way to check this when I first load my app?

16条回答
劳资没心,怎么记你
2楼-- · 2019-01-06 10:58

I think this will work:

 [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;

According to the UIDevice reference:
Quote:
"The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications"
I had initially assumed that this property contained the current orientation at all times, but not so, apparently. I guess that turning on notifications is being handled for us behind the scenes in other situations where the orientation property is typically accessed, so it wasn't obvious that this needs to be done manually inside the app delegate

查看更多
乱世女痞
3楼-- · 2019-01-06 10:58

for those who looking answer for Swift 3 or 4. just add that code inside of viewDidLoad() block.

`let orientation = UIApplication.shared.statusBarOrientation
 if orientation == .portrait {
        // portrait   
 } else if orientation == .landscapeRight || orientation == 
.landscapeLeft{
         // landscape     
 }`
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-06 11:01

the problem is that [UIDevice currentDevice]orientation] sometimes reports the the device's orientation incorrectly.

instead use [[UIApplication sharedApplication]statusBarOrientation] which is a UIInterfaceOrientation so to check it you'll need to use the UIInterfaceOrientationIsLandscape(orientation)

hope this helps.

查看更多
相关推荐>>
5楼-- · 2019-01-06 11:01

I still use this working code snippet for iphone 4:

-(void)deviceOrientationDidChange:(NSNotification *)notification{

//Obtaining the current device orientation
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];    

int value = 0;

if(orientation == UIDeviceOrientationPortrait)
{
    value = 0;

}else if(orientation == UIDeviceOrientationLandscapeLeft)
{
    value = 90;

}else if(orientation == UIDeviceOrientationLandscapeRight)
{

    value = -90;

}

CGAffineTransform cgCTM = CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(value));
[photoImageView setTransform:cgCTM];

}

查看更多
Viruses.
6楼-- · 2019-01-06 11:02

In order to obtain the orientation from the status bar it is also important to have the all the orientations enabled at the plist file.

查看更多
乱世女痞
7楼-- · 2019-01-06 11:02

Swift 3 based on @Marjin 's code.

var portraitOrientation = UIDevice.current.orientation == .portrait

if UIDevice.current.orientation == .unknown || UIDevice.current.orientation == .faceUp {
     portraitOrientation = UIApplication.shared.statusBarOrientation == .portrait
}

if(portraitOrientation)
{
     // Portrait
}
else
{

}
查看更多
登录 后发表回答