Auto Detect Device Orientation - ios

How to achieve the following functionalities in iphone app.
Always app launch portrait mode. if the simulator is landscape mode first launch in portrait mode then detect the device orientation change the app according to the current device orientation.

Either you can disable all orientations except portrait in your project and then set orientation programmatically throughout your app. Or you can stop orientation for specific view controller (may be in your case, viewcontroller during launching) by returning value NO. like this
- (BOOL)shouldAutorotate {
return NO;
}
and as mentioned by #Conner
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}

Related

iOS autorotate is not working when you have different orientation screens

I am working on an App which is written in Objective-C. I have more screens and all screens are Landscape Left and Landscape Right orientations and these screens must not be in portrait mode. But I have got 3 different screens which should be in only Portrait mode must not be in Landscape Left or Landscape Right.
This is the code for Landscape mode for all screens-
And this is for Portrait mode for my app
These are all I did in my View Controllers and In app Plist I have added needed orientations like This Info.plist
And the app device orientation will automatically changes. like this -
.
I am pretty sure all is clear and should work as it is expected but for some reason when I use the app and lock the auto rotation and run the app it is automatically opening in Portrait mode and when I unlock the autorotation it will be in Landscape and you rotate it will be rotated to Portrait mode. I used shouldAutorate return YES because it should rotate it automatically between Landscape Left and Landscape Right so I used it also the portrait mode screen is opening in Portrait but it is autorotating when user rotates the device.
Any help would be appreciated, Please share any idea why my app is not working as expected.
Following are the steps to fix this issue:
Remove orientations from plist because it overrides whatever logic you have in your view controller.
Keep below code where you have landscape orientations:
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
For portrait mode use it as below:
-(BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Now try locking the orientation your first view controller will appear in landscape and other one will be portrait regardless of orientation lock.

iOS+SpriteKit: How to force portrait mode?

I'm currently working on a SpriteKit game for iOS 7+ and XCode 6. The game should always be presented in portrait mode. So far I have implemented these methods in my view controller:
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
This works fine as long as I start the app holding my iPad in portrait mode. It does not switch the orientation during game play.
But when holding the iPad in landscape orientation during the app is starting, the game is shown in landscape.
How do I force the app to be shown in portrait mode even if the device was held in landscape mode during startup ?
To solve the app orientation issue
Go to your projects plist file (should be "YourApp-Info.plist")
Add a row and enter "Supported interface orientations"
Add the orientations that your app supports

UIApplicationInvalidInterfaceOrientation issue

My app should only support Portrait mode but in one single view controller I want to be able to use landscape.
If I set supported device orientations in project to Portrait only and run the app < iOS6.0 it works fine, but with iOS6 it crashes with:
'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES.
Turning landscape on in project properties will always rotate all other viewcontrollers even if I use shouldAutorotate (iOS6) or shouldAutorotateToInterfaceOrientation (pre iOS6).
Any ideas?
Got it. Used this
Enabled portrait and landscape orientation in project settings, used category in app delegate and override
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
in my landscape viewcontroller

Launch iPhone App in iPad with landscape mode

In the project summary, "Supported Interface Orientations" are all selected, as there is a photo gallery view in my App, which can be rotated with device. The other views are portrait only. The target devices is iPhone, and all things perform well in the iPhone. But when it runs in my iPad with landscape mode, the splash and the rootView are as following:
splash-landscape:
rootview-landscape:
What I expected look should be the same as the iPad is with portrait mode:
splash-portrait:
rootview-portrait:
The rootView is MyNavigationController, some related code is as following:
MyNavigationController.m
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return NO;
}
Please, correct your code with the following:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
It may seem odd returning YES from shouldAutorotate. The fact is, if you do return NO, then supportedInterfaceOrientations will not be called at all and your project settings will rule. You could as well remove shouldAutorotate and it should work just the same.
Reference:
When the user changes the device orientation, the system calls this method on the root view controller or the topmost presented view controller that fills the window. If the view controller supports the new orientation, the window and view controller are rotated to the new orientation. This method is only called if the view controller’s shouldAutorotate method returns YES.
Do you mean by showing a landscape launch screen and then in app still use portrait mode?
As far I know, iPhone-only app can't launch in landscape mode, which means giving a landscape launch screen to iPhone-only app is useless.
Check the document here at the "Providing Device-Specific Launch Images" section.
I guess what you want is make the status bar be portrait too. Unfortunately, there is no easy way to do this -- you can setup the device/interface orientation to protrait only, but it applies to the whole application. And you will need to process the orientation of all views by yourself. So, I will suggest you follow Hide status bar on launch image, hide your status bar, and use the same image in both orientations. It will make the splash screen look better.

iOS app orientation rotation lock / unlock by app's config

Hi,
I'm trying to implement orientation rotation lock / unlock toggle switch to my iOS app.
Locking is OK, but unlocking is a problem.
Suppose a situation that app's orientation and device's orientation are differ. If user unlocks in the situation, app's orientation should follow device's orientation immediately. But I cannot find the way.
How can I simulate device's orientation rotation?
Edit
I'll clarify the situation.
There is a toggle switch in app, enable/disable orientation rotation.
Step by step:
1. The switch is enabled.
2. Device rotates to portlait.
3. UIViewController's shouldAutorotateToInterfaceOrientation returns YES for every orientation.
4. App rotates to portlait.
5. User toggles the switch to disable.
6. Device rotates to landscape.
7. UIViewController's shouldAutorotateToInterfaceOrientation returns NO except portlait.
8. App doesn't rotate.
9. User toggles the switch to enable.
10. App should rotate to landscape. This is the problem.
I haven't tried it but there is a viewController method attemptRotationToDeviceOrientation (iOS5). Might be worth a shot. Interesting problem. Report back on if it works.
// call this method when your return value from shouldAutorotateToInterfaceOrientation: changes
// if the current interface orientation does not match the current device orientation,
// a rotation may occur provided all relevant view controllers now return YES from shouldAutorotateToInterfaceOrientation:
+ (void)attemptRotationToDeviceOrientation __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
See my answer here: https://stackoverflow.com/a/13033443/580173
I made a custom UINavigationController, and there you can check in which view you are, and respond with the right masks. (I wanted the root view to be portrait)
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if([self.viewControllers count] == 1) return UIInterfaceOrientationMaskPortrait;
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
You could read "Technical Q&A QA1688" which deals with autoratotion and in every viewcontroller make the message shouldAutorotateToInterfaceOrientation: return the correct value depending on your switch setting. There is also a switch in your info.plist. Marke sure you have the correct settings here as well

Resources