iPhone - getting device orientation in first view controller's view will appear

StackOverflow https://stackoverflow.com/questions/7651030

  •  06-02-2021
  •  | 
  •  

質問

I've only one app delegate and view controller in my app. The app works in both portrait and landscape modes. In my view controller's viewWillAppear method I'm getting the orientation of the device usisng following code:

UIInterfaceOrientationIsPortrait([[UIDevice currentDevice] orientation])

First time when I am running the application, even when my device is in portrait, its giving the result as landscape. Why is it so. How can I solve this?

役に立ちましたか?

解決

I have had a ton of problems with currentDevice.orientation Try this:

UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])

他のヒント

UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;


if (UIDeviceOrientationIsLandscape(deviceOrientation)
{
   //LandscapeView
}
else
{
   //PortraitView
}

From the documentation for UIDevice:

The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.

If you looked at the actual value being returned by 'orientation' you would see that it was 'UIDeviceOrientationUnknown'.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top