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?
UIWebView
viewWillAppear
Use the UIApplication property statusBarOrientation. You may have problems if you use the orientation of the device, if it is UIDeviceOrientationFaceUpor UIDeviceOrientationFaceDown.
statusBarOrientation
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
Example
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { // Do something when in landscape } else { // Do something when in portrait }
UIDeviceOrientation getCurrentOrientation() { UIDevice *device = [UIDevice currentDevice]; [device beginGeneratingDeviceOrientationNotifications]; UIDeviceOrientation currentOrientation = device.orientation; [device endGeneratingDeviceOrientationNotifications]; return currentOrientation; }
You need to convert UIDeviceOrientationto UIInterfaceOrientation.
UIDeviceOrientation
UIInterfaceOrientation
, -
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIDeviceOrientationPortrait) { NSLog(@"portrait");// only works after a rotation, not on loading app }
, , , , [[UIDevice currentDevice] orientation] .
[[UIDevice currentDevice] orientation]
, 2 -
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
rotate:
, interfaceOrientation viewWillAppear, - , - self.parentViewController.interfaceOrientation. [[UIDevice currentDevice] orientation], 100% (, ), , , .
interfaceOrientation
self.parentViewController.interfaceOrientation