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?
The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application's user interface. See “UIDeviceOrientation” for descriptions of the possible values. UIDevice.
You can set breakpoints in these functions and observe that interfaceOrientationChanged(to orientation: UIInterfaceOrientation) is always called after viewWillTransition(to size: CGSize) with the updated orientation.
Device orientation allows a device to detect its physical orientation with respect to gravity. If you've ever rotated a smart phone or tablet, and the screen has rotated in response, then you've seen device orientation in action.
Upside Down: This orientation means you change the orientation of your iOS device 180 degrees. But if you check this checkbox only, you will get the below error message when you run the app. So this checkbox should be used with other orientations such as Portrait or Landscape.
EDIT: I mis-read your question. This will allow you to start your application in certain orientations. Just realized you're trying to figure out the orientation on launch.
There is a method to check the status bar orientation on UIApplication
:
[[UIApplication sharedApplication] statusBarOrientation];
Try setting the application's accepted device orientations in the plist file:
<key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array>
This will indicate that your application supports Portrait (home button at the bottom), landscape left, and landscape right.
Then, in your UIViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)
method to return YES when the app should rotate:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight; }
This will tell the UIViewController to auto rotate if the device is in one of your supported orientations. If you wanted to support the upside-down orientation as well (portrait with home button on top) then add that to your plist and just return YES out of this method.
Let us know how it works out.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With