IOS: How to determine the current orientation in viewWillAppear?

I have UIWebViewthat I would like to load different URLs depending on whether its portrait or landscape. How can I find out that my current orientation is inside viewWillAppear?

+5
source share
4 answers

Use the UIApplication property statusBarOrientation. You may have problems if you use the orientation of the device, if it is UIDeviceOrientationFaceUpor UIDeviceOrientationFaceDown.

Example

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if (UIInterfaceOrientationIsLandscape(orientation))
{
   // Do something when in landscape
}
else
{
   // Do something when in portrait
}
+14
source
UIDeviceOrientation getCurrentOrientation() {
  UIDevice *device = [UIDevice currentDevice];
  [device beginGeneratingDeviceOrientationNotifications];
  UIDeviceOrientation currentOrientation = device.orientation;
  [device endGeneratingDeviceOrientationNotifications];

  return currentOrientation;
}

You need to convert UIDeviceOrientationto UIInterfaceOrientation.

+4
source

, -

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

, , , , [[UIDevice currentDevice] orientation] .

, 2 -

  • plist.
  • UIViewControllers shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation), YES, rotate:
+2

, interfaceOrientation viewWillAppear, - , - self.parentViewController.interfaceOrientation. [[UIDevice currentDevice] orientation], 100% (, ), , , .

+1

All Articles