Is there any way to differentiate between landscape and portrait when the device is sitting on a desk, i.e. UIDeviceOrientationFaceUp? I have two different xib files that I switch between based on the way the user is holding the device. But the problem arrises when I hold the device in landscape, and then place the device down. The device remains in landscape, but will load the portrait xib file. The problem would be opposite if in my coding I coupled the UIDeviceOrientationFaceUp with the landscape orientations: then I would have my landscape view showing when the device is still in portrait.
Is there any way to be able to figure out if the device is still showing landscape or portrait? I have tried checking frame width, but it claims it is 768 even if the device is sitting flat in landscape. And I can't force an orientation when the device goes flat, I hear thats the road to rejection on the App Store.
You can't do that. The accelerometer can't tell on which side of the phone the user is sitting when it's laid down on a table. You can measure rotation around it's axis using the gyroscope (and the compass), but that still doesn't tell you where the user is unfortunately.
There is one way to check it.
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.
Related
I want to initially launch one of the View controllers in JUST landscape mode, while the other views and the whole app can work in both portrait and landscape. How can I do it in iOS 7? Thank you.
So your whole app may supports all orientations but one view controller needs to be landscape only.
You can easily stop your landscape view controller to rotate from landscape to portrait but when the app is in portrait already then it is difficult to force the deivce to rotate as this contradicts the iPhone manufacturers priciple.
In fact, it is not really difficult when you know the trick. See my answer to this similar question.
Force controllers to Change their orientation in Either Portrait or Landscape
I am implementing iAd on iPad, and since I'm creating everything programmatically, I need to manually move the frame of the ad every time the user rotates the screen.
I uses the notification center to detect when the user rotates the device.
The problem is, sometimes when I rotate the device too fast the orientation becomes "UIDeviceOrientationUnknown". In this case, how can I tell the screen orientation to adjust the ad position accordingly?
I do not wish to use auto-layout.
I normally ask the status bar for orientation, 'cause it's always either landscape or portrait, like so:
[UIApplication sharedApplication].statusBarOrientation. In any case, there are a few approaches. I recommend reading through this thread.
I have encountered a problem developing an iPad app with xCode.
When I load a view into my rootViewController like this
[self setView:viewController.view];
The view is always displayed in Portrait even though the device is rotated to landscape.
I checked all options and tried changing the shouldAutoRotateToOrientation method and it seems to have nothing to do with it.
If I rotate the device to portrait and then back to landscape it works.
Does anybody know why this is happening?
Best regards,
Matteo
The problem was that I was nesting viewControllers which leads to bad behavior like rotation notifications not being sent to certain ViewControllers.
I wonder how to perceive orientation changes even if the device's current orientation is locked (by double clicking home button and pressing the grey orientation lock icon)
However, I've seen video taking apps (UIImagePickerController) that can perceive orientation changes and change its control orientation.
I know that for detecting the change, I can use:
- shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
- UIDeviceOrientationDidChangeNotification
but neither of those methods work if the orientation of the device is locked.
You could use the accelerometer to get the current orientation check this Detect iPhone screen orientation
My iPad app displays a movie full screen using the convenient MPMoviePlayerViewController class. I present it like this:
[self.hostController presentMoviePlayerViewControllerAnimated:playerViewController];
And later, when notified that playing is done, I dismiss it like this:
[self.hostController dismissMoviePlayerViewControllerAnimated];
It works fine, except when the user rotates the iPad during movie playback:
1- The iPad is in vertical orientation. My view is vertical.
2- The user starts the movie in vertical orientation. The player is vertical.
3- The user rotates the iPad to the horizontal orientation.
4- The player switches to its horizontal orientation. so far so good.
5- The movie stops, the player is dismissed, my reappears, the iPad is still horizontal, but my view has stayed in the vertical orientation is was in step 1. Now it looks sideways.
Of course, if the user then rotates the iPad, it's back to normal. My view then rotates normally as the iPad rotates.
Did anyone ever encounter that? An easy fix?
Thanks.
I've encountered this numerous times and it appears to be an Apple bug (and have reported it as such).
The only way (I've found) around this is to listen to UIDeviceOrientationDidChangeNotification and UIApplicationDidChangeStatusBarOrientationNotification notifications. Use the relative timestamp on these notifications; if they both occur within a second of each other, you can be sure that the status bar change is a result of the user switching orientations and not the media SDK changing the status bar orientation. Then, when the movie is finished, you can tell your view controller that it should rotate to landscape.