Force a UIViewController just landscape in iOS 9 - ios

I have an app supports both orientation portrait and landscape. But in a viewcontroller I JUST want it present in LANDSCAPE mode. I try overriding some methods for changing orientation such as
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return (UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
return YES;
}
return NO;
}
But none of them are called in iOS 9. Therefore, this viewcontroller present both portrait and landscape. On contrary, it works perfectly in iOS 8.
So annoying, is there anyway to force just a viewcontroller presented in landscape mode in iOS 9
UPDATE:
As Ronak Chaniyara's answer, I solved my problem, just one controller is in landscape mode.
Now I face another problem. If I want to force one controller just in portrait mode, I implement these methods in the controller but it's still rotated if I rotate screen.
Is the solution still work with portrait mode, or I have to find another approach to force a controller just in portrait mode

I think issue will be you have 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.
Hope this helps.

Look at your info.plist file. Here you have a key named "Supported Interface Orientation" with a group for the iPhone version and another for the iPad.
Here you can delete the value "Portrait (bottom home button)", and replace it by "Landscape (left home button)" or "Landscape (right home button)".
I hope it helped!

In this way, you can do:
Go to target setting
Go to development info
Change device orientation to landscape and uncheck all other option.
Hope this help you

Related

Locking View Rotation to Landscape for whole app in ios 8.1

I'm new to IOS development, and I'm trying to write an app for deployment on an ipad. Partly to keep things simple, layout-wise, and because I believe that my users will only use the app in landscape mode, I wish to only allow landscape views, and completely disable portrait views.
I've found a good deal of advice looking around the internet for an answer. Unfortunately, none of it has worked for me. The best answer I've found was to simply to go the target in xcode, and under "deployment info" -> Device Orientation, simply uncheck "Portrait" and "Upside Down". This should, theoretically, solve my issue, but unfortunately, it does not. The view rotates just as normal to portrait mode.
Going to the info tab and setting the Initial Interface Orientation to Landscape (Left) does make the app at least start in Landscape mode, but it does not restrict it only to that mode.
Even adding
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
into my main view doesn't seem to help anything. So I'm somewhat stumped. There must be some setting or something somewhere which is still allowing portrait views. What might this problem be?
If you look at your AppName-Info.plist file, there should be a key titled 'Supported interface orientations'.
You should remove any of the Portrait values in that dictionary and make sure you only have the Landscape values included!
Edit:
In the question-asker's case, their issue involved a piece of code in the AppDelegate that changed the app's supported orientations.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
/* They had the following:
* return UIInterfaceOrientationMaskAll;
* Which allowed the orientation to rotate to portrait
*/
// This fixed their issue:
return UIInterfaceOrientationMaskLandscape;
}
Hope this helps!
You can do this in General tab in your project

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.

How to make an iOS app Landscape ONLY

Seems like a simple thing to do right? Go to the Target's Summary tab and set "Supported Interface Orientations" to Landscape Right only. You would think that would mean the app would be landscape only. But no.
On a device running 5.1 for example, if you open the app and hold the phone in the correct landscape right position you will see the view rotated 90 degrees counterclockwise, as if it think it's supposed to be in portrait mode, unless you add something like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
But that only works for one view controller, and who knows if it works on all OS versions.
So how do you make an app landscape only? Do I have to add that code to every single view controller in the app? I do not need any rotation. All my views are designed in Landscape. I just want the app to open in Landscape Right mode and stay that way. And I want to support iOS 5 and up.
There are new methods introduced that you have to implement along with the old one they are as below
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
In Xcode you can navigate to your project settings -> summary -> iPhone/iPod deployment info. Here you can select the supported interface orientations. You can also edit the 'Supported interface orientations' array in your application's info.plist by adding the desired interface orientations application wide.
In the info.plist put the orientation to landscape

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.

Universal app not rotating on iPad but does on iPhone

I have just completed building a universal app which rotates perfectly on my iPhone, but on the iPad it just stands still.
Similar to this question, but that does not solve my issue.
The supported interface orientations are all set to allow rotation and I have even set this in my app delegate:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
Still the iPhone rotates fine, but the iPad won't move from portrait.
What could cause this, or rather how can I fix it?
[UPDATE]
The switch on the side is not on lock.
The orientations in the PList are set correctly.
The Project settings (where you select it via buttons) are set correctly.
#adam-s was right, except with Xcode 7.x there is no button below the "devices" selector.
With Xcode 7, you need to change "Universal" to "iPad" whereby the orientation selectors change to reflect iPad-only settings. Then you can change the selector back to "Universal".
Confusing!
Don't forget to change the rotation settings for the target for both iPhone and iPad - note that there's an iPad button to the right of the iPhone one (which some people, such as myself, might miss at first glance):
I fixed this by adding this piece of code to every ViewController of mine:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
It seems like this question's answer was correct after all. I just thought it wasn't since I checked through all the ViewControllers and found nothing restricting it from turning.

Resources