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

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

  •  06-02-2021
  •  | 
  •  

Frage

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?

War es hilfreich?

Lösung

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

UIDeviceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])

Andere Tipps

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'.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top