How to support only portrait mode on an iPhone app - iphone

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.

Related

rotate my iphone app in ipad when rotate option is enabled

I want to rotate my iphone application in ipad if I rotate the ipad
what I use in my code is :
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (orientation == UIInterfaceOrientationMaskAll);
}
and I check both portrait and landscape:
and the plist file:
my application device family is only iphone, and appears as an iphone in ipad device
But it doesn't rotate, please correct my code above, thank you
you ask it ? ;
if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPhone) {
}
Put it into the code if try again.
In the .plist file you only specify your startup orientations. After that every view controller can implement shouldAutorotateToInterfaceOrientation: in which the view controller is "asked" if a rotation to another orientation is acceptable. In the standard template for iPad apps this always returns YES and thus allows all orientations. In your case you might only return YES when the given orientation is UIInterfaceOrientationLandscapeLeft, although you should have a look if you can support both landscape orientations as Apple human interface guidelines strongly suggest to at least support both landscape orientations if one is supported.
Note that every view controller in your app has to specify its own orientations as it may make sense to have some views more restricted than others.
For further information on this have a look at:
Supporting orrientations for iPad apps: http://developer.apple.com/library/ios/#qa/qa1689/_index.html
Why won't my UIViewController rotate with the device: http://developer.apple.com/library/ios/#qa/qa1688/_index.html
UIViewController class reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html

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.

iPhone application landscape mode doesn't work

Currently I have only portrait mode in iPhone application. I would like to permit landscape mode for one controller in application but unfortunately I'm not able to make it work.
I have these settings in my plist file:
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
</array>
And I implemented these methods in my controller:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
I use navigation controller in the app so I realize that it might be the problem. I subclassed the UINavigationController and implemented again shouldAutorotateToInterfaceOrientation, supportedInterfaceOrientations and shouldAutorotate in the same way as above. Of course for I use subclassed navigation controller to show the controller which should have ability to rotate. I also tried to overwrite the methods of navigation controller by category but it also doesn't work.
I don't work on the application from the beginning, I inherited the codes. If there is some possibility how to restrict landscape globally it might be my case. For testing I use iPhone 6.0 simulator but my deployment target is 5.0. I appreciate any tips / help.
iOS6 also requires that you set a rootViewController for your window rather than adding the controller's view as a subview.
If you are doing something like this:
[window addSubview:someController.view];
then you can try changing it to this:
[window setRootViewController:someController];
Also look at this link. For ios6, Apple introduced these two methods :
supportedInter
faceOrientations:
shouldAutorotate
Hope it helps.
Shumais Ul Haq
Try this on Controller youwant to give landscape orientation
// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation); // support only landscape
}
Go to target window.
Click on Summary tab.
iPhone/iPad Deployment info -> Set the supported interface orientations for iPhone & iPad as per your requirement. See following image for more reference.

How can I disable landscape orientation?

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.

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