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

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

Related

iOS 9 supportedInterfaceOrientations not working

I have a UIViewController with the following code:
- (BOOL) shouldAutorotate {
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationPortrait;
}
I am not using a UINavigationController. When this UIViewController is being displayed, the device will still rotate to landscape. I am targeting iOS 9, what's the issue here?
So the issue was that I had defined the allowed orientations in info.plist which apparently overrides anything you do anywhere else throughout the project.
To correct the issue I removed the entries from info.plist and defined them in the project settings. Now everything works as expected.
I don't think Bryan's answer works,for changing the orientations in project settings also changes the info.plist as #mrhangz commented.
If the issue is iOS9 only,it is probably due to the new feature of iOS9 in iPad called Split view.The iOS9 enable Split view by default in particular iPad device,see Apple documents here.
The split view forced your app to support all orientations in all view once adoptted.So if you set all orientations support in either info.plist or target general setting,and then split view is supported by default,which will ignore the orientation setting though supportedInterfaceOrientations in your viewController and support all orientations.
As the document written,if you checked Requires full screen in your target settings,then your app will not support split view.Now you can control orientations in code again.
I have try many solution, but the correct answer with working solution is:
ios 8 and 9, no need to edit info.plist.
- (BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
possible orientation
UIInterfaceOrientationUnknown
The orientation of the device cannot be determined.
UIInterfaceOrientationPortrait
The device is in portrait mode, with the device held upright and the home button on the bottom.
UIInterfaceOrientationPortraitUpsideDown
The device is in portrait mode but upside down, with the device held upright and the home button at the top.
UIInterfaceOrientationLandscapeLeft
The device is in landscape mode, with the device held upright and the home button on the left side.
UIInterfaceOrientationLandscapeRight
The device is in landscape mode, with the device held upright and the home button on the right side.
In swift 5
The code below will lock the current view controller into portrait mode but still allow the other view controllers to transition to landscape. I do believe that you have to enable all the orientations at the project level and then turn then "off" using this method but am not sure if there is way to turn them back "on" one by one.
private var _orientations = UIInterfaceOrientationMask.portrait
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get { return self._orientations }
set { self._orientations = .portrait }
}
A more thorough explanation of it all can be found here:
The supportedInterfaceOrientations method doesn't override any method from its superclass
For simplicity, for iPad, if Supported interface orientations (iPad) property in info.plist includes all the four orientations, with UIRequiresFullScreen property value as NO, iOS will treat your app as supporting split view. If an app supports split view feature, you can not disable it from rotating, at least by the ways above.
I have a detail answer here.

Autolayout persists on simulator - XCode

I'm having some issues with the rotate/autolayout feature.
It's an Universal App, Supported Interface Orientations just Landscape.
Autolayout uncheck in both storyboards.
But, iPad simulator still rotating and moving around all items once it gets "physically" rotated...
How can I stop this Autolayout for real?
Thank you very much
You can put these methods in individual viewControllers to control the supported orientations or whether or not they will auto-rotate.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
//or UIInterfaceOrientationMaskPortrait, depending on what you want
}
As a note, the above method is good enough to support both landscape orientations. Including the below method will lock the screen to just ONE of the four orientation schemes. (Turning the device 180 degrees won't rotate it--it will appear upside down.)
- (BOOL)shouldAutorotate
{
return NO;
}

Auto Detect Device Orientation

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;
}

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 - UISupportedInterfaceOrientations and shouldAutorotateToInterfaceOrientation

I am looking for some clarification on how to allow only certain orientations for your iOS app. I am aware of UISupportedInterfaceOrientations and shouldAutorotateToInterfaceOrientation but I am a little confused on their uses and exactly how they fit together.
I attempted to use UISupportedInterfaceOrientations to only allow landscape orientations, which appeared to have no affect until I researched in to it and read that it affects the initial orientation. Upon testing this, my app does appear to only open in landscape but quickly rotates if the screen is portrait.
I know you can use shouldAutorotateToInterfaceOrientation to limit the allowed orientations, for example:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
However, while doing some reading online I read shouldAutorotateToInterfaceOrientation is deprecated as of iOS6.
Basically my questions are:
What is the correct approach for limiting screen orientations across
multiple versions of iOS?
Is the only use of UISupportedInterfaceOrientations to limit the
initial orientation?
Edit:
To expand on the accepted answer, shouldAutorotate works in iOS6. As a quick fix if you've already implemented your logic in shouldAutorotateToInterfaceOrientation and/or you want to support earlier versions of iOS, you can do the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotate {
return [self shouldAutorotateToInterfaceOrientation:self.interfaceOrientation];
}
The method you need to use for rotation instead of shouldAutorotateToInterfaceOrientation is just shouldAutorotate
Handling rotation, according to the AppleDoc for ViewControllers:
In iOS 6, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. Generally, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen; child view controllers use the portion of the window provided for them by their parent view controller and no longer participate in directly in decisions about what rotations are supported. The intersection of the app’s orientation mask and the view controller’s orientation mask is used to determine which orientations a view controller can be rotated into.
You can override the preferredInterfaceOrientationForPresentation for a view controller that is intended to be presented full screen in a specific orientation.
The method shouldAutorotateToInterfaceOrientation is deprecated, as are some methods for handling responses to device rotation.
For supporting methods of multiple versions of iOS, Here's something else that Apple has said:
For compatibility, view controllers that still implement the shouldAutorotateToInterfaceOrientation: method do not get the new autorotation behaviors. (In other words, they do not fall back to using the app, app delegate, or Info.plist file to determine the supported orientations.) Instead, the shouldAutorotateToInterfaceOrientation: method is used to synthesize the information that would be returned by the supportedInterfaceOrientations method.
Taken from release notes
To answer your second question:
Yes, the "UISupportedInterfaceOrientations" entry in the Info.plist is used only for initial startup of your app, making sure it doesn't start your app in an orientation it doesn't support, so that it's not required to perform a rotation right away.
Also, overriding "application:supportedInterfaceOrientationsForWindow" in your AppDelegate is pretty useful if your app never wants to use particular orientations (e.g. for a game that only does landscape).
Lastly, and this is a common error, on iPhone and iPod Touch devices, the device should never rotate to UIInterfaceOrientationPortraitUpsideDown! That's because these devices (contrary to an iPad) do not let the user lock a device in landscape mode with the Lock soft button - that button only locks into portrait. Therefore, if a user, laying on his side, wants to use an app in landscape mode, he cannot do this if your app goes into the upside down orientation. But if you disallow that rotation, then it works.

Resources