How can I disable landscape orientation? - iphone

I am making an iPhone app and I need it to be in portrait mode, so if the user moves the device sideways, it does not automatically rotate. How can I do this?

To disable orientations for a particular View Controller, you should now override supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation.
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// Return the orientation you'd prefer - this is what it launches to. The
// user can still rotate. You don't have to implement this method, in which
// case it launches in the current orientation
return UIInterfaceOrientationPortrait;
}
If you're targeting something older than iOS 6, you want the shouldAutorotateToInterfaceOrientation: method. By changing when it returns yes, you'll determine if it will rotate to said orientation. This will only allow the normal portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
// Use this to allow upside down as well
//return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Note that shouldAutorotateToInterfaceOrientation: has been deprecated in iOS 6.0.

Xcode 5 and above
Click your project in the Project Navigator in the left sidebar to open the project settings
Go to the General tab.
Uncheck the options you don't want in the Deployment Info section, under Device Orientation

Xcode 4 and below
For those who missed it: you can use the project settings screen to fix orientations throughout the app (no need to override methods in every controller):
It's as simple as toggling the supported interface orientations. You can find by clicking on your Project in the left panel > the app target > Summary tab.

Most simple solution separate for iPhone and iPad (Universal) - its remove unnecessary orientation in the info.plist file or Project -> Info -> Custom iOS Target Properties.
Just add or remove orientation item from list:
Supported interface orientation for iPhone
Supported interface orientations (iPad) for iPad

In Xcode 13.3.1, simply unchecking undesired orientations does not prevent an app from supporting all rotations. It is necessary to enter the Build Settings tab and manually remove any orientations from the following fields that you do not wish to support:
In my case, my app will now only support portrait orientation.

If you want to disable landscape orientation for both iPhone and iPad.
Go to Targets and Go to the General tab. See the below screen and deselect landscape left and landscape right.
Here in this case only iPhone landscape mode will be disabled not for iPad. For iPad all modes are anabled. If you want select device option from Universal to iPad. It will looks like this. See below screen.
Now you need to deselect all modes except Portrait for iPad. See below screenshot.
Now you successfully disabled all modes except Portrait for all devices.

If you created a new Xcode 13.3 project and unchecked unnecessary orientation checkmarks in the Project > General > Deployment and it didn't help. Check the Target > Build Settings - there are 2 rows which override global settings.

Swift 3
If you have a navigationController, subclass it like this (for portrait only):
class CustomNavigationViewController: UINavigationController {
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
return UIInterfaceOrientation.portrait
}
}

Removing the method shouldAutorotateToInterfaceOrientation from your class entirely also works. If you don't plan on rotating then it makes no sense to have the method in your class, the less code the better, keeps things clean.

Xcode 8, Xcode 9, Xcode 10 and above
Also, make changes in Info.plist file

I've had the same problem on Xcode 13.0 even though I set the device orientation only Portrait.
Adding these 2 lines to Info.plist solved my problem.

Related

set cocos2-x project to portrait orientation

I am trying to set up my project with portrait orientation, but I am just getting landscape. I am using cocos2d-x (2.1.4).
I have set, as said here:
At RootViewController.mm:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsPortrait( interfaceOrientation );
}
// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
return UIInterfaceOrientationMaskPortrait;
#endif
}
I also tried changing the settings, still nothig.
setDeviceOrientation does not exist anymore...
Anyone knows how I can set the project to portrait??
UPDATE: It seems to work on the device, it only happens on the simulator
I do it this way: In xcode at the top of the file list is the project. Go to that, then the "Targets > whatever" bit. You have "supported Interface orientations". You can check off which orientations you want and don't want. Make sure to note that there are 2 different sections for iPhone and iPad, you'll have to scroll down and set it separately in the iPad orientation section.
I'd imagine the reason it doesn't work in the simulator is that you're using an iPhone/iPod for testing and running the iPad simulator.

Force landscape mode at the beginning of app, but allow later changes in orientation

I am planning to allow user to rotate the devices however during launch, i want the app start from landscape mode. May i know how can i do that?
Here is my code now for the orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
You need to check the orientation of the status bar.
Please check the following links :-
Force iOS app to launch in landscape mode
Modal View Controller force Landscape orientation in iOS 6
Force Landscape Orientation on iOS 6 in Objective-C
In the Project settings/target settings (the blue icon towards the top of the navigation bar on the left, in xcode), there are settings for specifying the orientation. You'll see 4 images, each of which can be selected or deselected.
Select only the one that you want initially for your app, and then you can use the shouldAutorotate and supportedInterfaceOrientation methods to allow or disallow certain orientations.
Supported interface orientations are also specified in the plist. Makes sure to keep the initial orientation for the app at the top of that supoortedInterfaceOrientations list in the plist file.

iPhone App with only portrait mode

How can i make my app to be always in portrait mode only. If i rotate iPhone it should not display app in landscape mode.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortrait);
return YES;
}
After this coding when i rotate iPhone app is displayed in landscape mode. How can i make app to be in portrait mode all the time.
Remove the method — you don't need it. An iPhone app always starts in portrait mode unless you force it to start in landscape mode. If this method is absent, it will stay in portrait mode, as it effectively means "My view won't respond to interface orientation changes."
Incidentally, this method actually starts out commented out in Xcode's view controller templates, which is the same as it not being there at all.
In xcode click left top on your project name and then right below of "TARGETS" you can set "Supported Device Orientations".
Seems for iOS 6 you need to override this method,
-(BOOL)shouldAutorotate {
return NO;
}

How to support only portrait mode on an iPhone app

I have a strange problem in an iPhone app I'm developing. I want my app to support ONLY portrait mode, but for some reason I can't do it (device & simulator).
To support only portrait mode I did as follow:
In the TARGET summary section on Xcode, I chose only portrait.
All my ViewControllers implements shouldAutorotateToInterfaceOrientation
But as I said it won't work, and the strange result is that the app support ALL the orientations (portrait, upside down, landscape left, landscape right).
Any ideas?
this how I implement shouldAutorotateToInterfaceOrientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
NSLog(#"Checking orientation %d", interfaceOrientation);
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
I notice just now that when I rotate the phone I get this message:
"Two-stage rotation animation is deprecated. This application should
use the smoother single-stage animation."
What does it means?
On the Target Summary choose portrait only.
Go to info.plist file. Right Click open it as source code. And look for this line. For me in iPad its like this:
<key>UISupportedInterfaceOrientations~ipad</key>
Delete all other orientation and keep the only one which you need..Like this :
<array>
<string> UIInterfaceOrientationPortrait </string>
</array>
It is possible to have multiple ViewControllers on the screen. The UITabBarController is itself a UIViewController, and it only passes shouldAutorotateToInterfaceOrientation: requests to the viewControllers within if it chooses. The default implementation does this, but if you subclass it, the code XCode generates (as of iOS 5.1) does not.
check your plist and make sure the key there is set correctly.

sencha touch :: how to supress orientation change

In my sencha-tpuch / phonegap app on iOS I want my app not to change orientation with the device its used on. how could I suppress orientation change in sencha-touch or what would I override exactly?
thnx!
You dont need to edit any code. All you need to do is go to your configuration files and specify orientations you want to support. By default all 4 orientations are supported, you have to edit it to support orientations you want. Rebuild the application and Go.
The entry should be in one of the following files:
phonegap.plist/application.plist/config.xml
Also check this link.
With only Sencha Touch, as it cannot force browser not to change orientation, it is NOT possible to remove orientation change. See this link.
In your phonegap xcode project find the file MainViewController.m in there find the function
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
And return yes for supported orientations.