Both on iPhone simulator and iPhone 3GS (iOS 6) I cannot manage to set the orientation to portrait upside down. I have just one ViewController. I've added this code in it:
-(BOOL) shouldAutorotate{
return YES;
}
-(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortraitUpsideDown;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
if (toInterfaceOrientation==UIInterfaceOrientationPortrait || toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
return YES;
}
return NO;
}
I also added this to AppDelegate.m:
-(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown;
}
I've also checked the plist file, both orientations are present there. On my storyboard, at Simulated Metrics Orientation is set to Inferred. I do nothing in my applicationDidFinishLaunchingWithOptions, except return YES. I've just added a UIImageView inside of this ViewController. Is it possible to make this orientation work?
supportedInterfaceOrientations returns an NSUInteger. It returns all of the interface orientations that the view controller supports, a mask of interface orientation values.
You need to return values defined in UIInterfaceOrientationMask Enum, like the following shows:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
I know there's an answer that worked but for anyone else who's still stuck with the same issue:
I had a similar problem that was connected to Xcode: portrait rotations weren't being supported despite returning YES to - (BOOL) shouldAutorotateToInterfaceOrientation: for all cases. It turned out to be the enabled 'Supported Interface Orientations' in the summary window of my target's project editor:
The above 'iPhone/iPad Depoloyment Info' selections weren't doing it, it was the 'iPad Deployment Info' section that appears to control what the simulator will do despite the fact that I was only using the iPhone sim. Once I'd enabled the same orientations in that section then the iPhone simulation worked and I was able to test what happened when the simulator was rotated....
I tried many ways for this. It seems only one way it works, but globally through the app, not only for particular view controller.
First, you have to check on Upside Down of Device Orientation in target general settings.
Then, in navigation controller extension, you override the supportedInterfaceOrientations method
extension UINavigationController {
override public func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return .All
}
}
Related
My application always launches in landscape mode with the home button on the left side. If the home button on the right side it rotates. How do i make it opposite? I tried setting different values into info.plist file for initial interface orientation key but it didn't work. I tried switching order of values in this method:
- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
but it didn't work neither. How do i do that?
For IOS 5 and 5.1 :
Try to set (BOOL)shouldAutorotateToInterfaceOrientation in your view controllers, it works for me.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;
}
If you are using storyboards you can also set your initial and other viewcontrolers to landscape mode :
For IOS 6:
your (NSInteger)supportedInterfaceOrientations should be (NSUInteger) , I am not sure though I never use it.
// Only used by iOS 6 and newer.
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination to supported by Viewcontroller.
return return UIInterfaceOrientationMaskLandscape;
}
Currently i have this code in all my views which i don't want to go into landscape which is all except for one:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
-(NSUInteger)supportedInterfaceOrientations
{
if (self.selectedViewController)
return [self.selectedViewController supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
return YES;
}
this is the code in my tab bar controller:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
-(NSUInteger)supportedInterfaceOrientations
{
if (self.selectedViewController)
return [self.selectedViewController supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotate
{
return YES;
}
And lastly the code in the viewcontroller that i want to go into landscape (because there is a video):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
The views are all rotating like i want them to, the only problem is when the viewcontroller with the video in rotates and you then click on another view that view will stay in landscape, what i want to happen is this view to rotate straight into portrait without the user rotating the device into portrait. Anyone know what code i need?
This can be a difficult one to solve elegantly. Forcing orientation can be breaking Human Interface Rules rules or in the past just required calling private API's
I would recommend attempting a different approach which has worked for me in the past. When the user rotates the one viewcontroller that displays a video allow that to rotate but at that time remove any way the user can navigate away and then return those navigation controls when they rotate the device back to portrait. For me it was simply hiding the navigation bar until they returned to portrait. I like this because it is very clear to the user that they have to return the phone to portrait before moving on and they won't be surprised with navigating somewhere else that is magically another orientation.
I am working on iPhone application with both Portrait orientations support (Portrait and Portrait UpsideDown).
In earlier XCode4.5.1, I have resolved this issue by:
Setting rootViewController in AppDelegate
Mentioning shouldAutorotateToInterfaceOrientation like this:
-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation); }
Mentioning supportedInterfaceOrientation in info.plist file
Now I am doing the same things for newer XCode but in iPhone simulator v6.0 its not supporting rotation properly.
I have tried with these methods as well:
-(BOOL) shouldAutorotate {
BOOL returnValue = NO;
int interface = [self preferredInterfaceOrientationForPresentation];
if(UIInterfaceOrientationIsPortrait(interface)) {
// Code to handle portrait orientation
returnValue = YES;
}
else {
// Code to handle Landscape orientation
returnValue = NO;
}
return returnValue;
}
- (NSUInteger) supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return (UIInterfaceOrientationPortrait |
UIInterfaceOrientationPortraitUpsideDown);
}
Please guide me how to support both the Portrait orientations for iOS > 4.3 all the versions.
Thanks in advance,
Mrunal
Why Did Orientation Change to Landscape Stop Working in iOS 6?
Starting in IOS 6.0 there are several orientation changes that stopped my app from rotation out of Portrait.
The fix for me, and the one applicable here, is that you must setRootViewController on the window in your AppDelegate. The earlier answer offers several suggestions that are all correct, but misses the one item that was relevant for me.
In application didFinishLaunchingWithOptions, after:
[window makeKeyAndvisible]
or
[window addSubview: viewController.view];
You must replace with:
if([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
[window setRootViewController:viewController];
} else {
[window addSubview: viewController.view];
(or [window makeKeyAndvisible])
}
You also need to add the new shouldAutoRotate instead of the depreciated shouldAutorotateToInterfaceOrientation, but this was easier to find and less crucial for me.
Same with making sure all your orientations are specified in your .plist file.
I did not need to override supportedInterfaceOrientations because I am satisfied with the default orientations (all for iPad UIInterfaceOrientationMaskAll, all but upside-down for iPhone UIInterfaceOrientationMaskAllButUpsideDown).
I have to admit, that when iOS 6 came around, I fiddled around with this until I got something to work, and then stopped. But, here is my guess:
I believe that shouldAutorotate is intended to be used in situations when you might want to dynamically change whether or not your app supports autorotation at all. I don't think it's intended for you to tell iOS which orientations you support (like the original shouldAutorotateToInterfaceOrientation: method). So, if your app supports any autorotation, I think you should just do this:
-(BOOL) shouldAutorotate {
return YES;
}
supportedInterfaceOrientations is where you are supposed to identify which orientations you support.
However, in that method, you're doing that differently than what I've been using (which might, or might not be a problem). I think you should use the mask constants, for this method. So, instead of this:
- (NSUInteger) supportedInterfaceOrientations {
return (UIInterfaceOrientationPortrait | UIInterfaceOrientationPortraitUpsideDown);
}
use this:
- (NSUInteger) supportedInterfaceOrientations {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
Also, I don't think the preferredInterfaceOrientationForPresentation method is supposed to return more than one orientation. Preferred should probably just be one, such as:
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
Here is the relevant Apple documentation
Update:
The other thing you might try is using this method:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown);
}
in your App Delegate class. This method is optional, but can set the default values for which orientations your app supports, in case you don't specify them in all your View Controllers, or in the app's plist file. Since you don't show your plist file, and your other View Controllers, I can't tell if this might help, or not. But, it might be something to try.
When Game center is loaded its default orientation is portrait.
In order to lock it in landscape mode, added a category.
#implementation GKMatchmakerViewController (LandscapeOnly)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
return NO;
}
#end
It is working fine in below iOS 6 .But in iOS6 it shows an error.
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and shouldAutorotate is returning YES'
Please explain a solution.
At last i avoided crash by following the workaround mentioned in Apple's iOS 6 release notes.
Workaround:
1.Apps should provide the delegate method application:supportedIntefaceOrientationsForWindow and ensure that portrait is one of the returned mask values.
- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
2. When a UIBNavigationController (or a UIViewController) is involved, subclass the UINavigationController/UIViewController and overriding supportedInterfaceOrientations.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
And
In buid summary supported orientations selected landscape right and landscape left.
Now game center is working properly without crash.
Have to add 1 little thing. Struggling with that stupid issue about 2 days. If above doesn't help and you have UINavigationController invovled (and you already did subclass it) you need the following (in appDelegate):
[window setRootViewController:navigationController]; // use this
// instead of [self.window addSubview:navigationController.view];
thanx 2 http://grembe.wordpress.com/2012/09/19/here-is-what-i/
I updated to Xcode 4.5 and am working with iOS6--a mistake I will definitely not make next time there's an update; it's been sort of nightmarish for somebody so new to iOS--and I've just noticed an app I'm working on is autorotating. I never noticed it autorotatin before the update, but it's also possible I just didn't rotate the phone while testing, so I can't be sure. I've added the following code to the main UIViewController and it's still rotating:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
Is this the right way to disable autorotation? If it is, then maybe there's some change in iOS6 and I'll have to wait until the full release to find out. But if I've gotten it wrong, what code should I use instead?
Thanks, as always, for your help.
EDIT: Here's the code I changed it to, but it's still rotating. Have I gotten it wrong?
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait)
{
return YES;
}
else
{
return NO;
}
}
that is because there was never a success. You should choose one of the orientations.
Hold command and click on UIInterfaceOrientation you will see an enumeration of the possible options.
then you can test against those to decide your YES Scenario.
I may have originally misunderstood your problem. It seems that you may have been saying that your app is allowing rotation. but the code should disallow that.
I was thinking you were saying it was still firing the code. Trying to find a Yes
One thing to think about. is there may be more than one view controller available. perhaps your code is not being hit.
A couple of possible issues for this.
Your code is not even being used. because the view is being allocated as UIViewController as opposed to your custom view controller.
You code is being used but that View controller is not the one being asked about the Orientation. therefore that specific code is not being hit.
A bad build keeps putting the wrong assemblies onto the device.
Your solutions can be as follows.
Ensure your code is the one being allocated. Either there is a direct alloc on your custom class. or the xib file is inflating it. Check out the Identity Inspector when you have your xib file open. select your View Controller and ensure that custom class is set to your class type
Look at the hierarchy. what other view controllers are there. Perhaps one of those are telling the app it can autorotate to any orientation.
Find your "DerivedData" folder and remove it entirely. Sometimes this works from the organizer. other times you need to delete directly off the disk. Then clean and build again.
Also another solution could be as simple as setting the settings in the Project file.
Select your project file from the file browser and you will see the iPad and iPod settings in the summary. You can "UnPress" buttons for the orientations that you want to disallow. and any view controllers that you do not otherwise code orientation into. will use these by default.
My apologies for the confusion.
Update
I commonly use this code to handle my autorotation.
It not only differentiates the ipad from the other ios devices, but it also forwards the request onto presented controllers so a view that is shown modal may respond how it wants.
Orientation is a pain when you dont understand it :)
// Detect iPad
#define IS_IPAD() ([[UIDevice currentDevice] respondsToSelector:#selector(userInterfaceIdiom)] ? \
[[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad : NO)
// Set preferred orientation for initial display
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
if (IS_IPAD()){
return UIInterfaceOrientationLandscapeRight;
}
else {
return UIInterfaceOrientationPortrait;
}
}
// Return list of supported orientations.
- (NSUInteger)supportedInterfaceOrientations{
if (self.presentedViewController != nil){
return [self.presentedViewController supportedInterfaceOrientations];
}
else {
if (IS_IPAD()){
return UIInterfaceOrientationMaskLandscapeRight;
}
else {
return UIInterfaceOrientationMaskAll;
}
}
}
// Determine iOS 6 Autorotation.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
// Return yes to allow the device to load initially.
if (orientation == UIDeviceOrientationUnknown) return YES;
// Pass iOS 6 Request for orientation on to iOS 5 code. (backwards compatible)
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
// handle iOS 5 Orientation as normal
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
// Return YES for supported orientations
if (self.presentedViewController != nil){
return [self.presentedViewController shouldAutorotate];
}
else {
if (IS_IPAD()){
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
else {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
}
}
Rotation APIs have changed in iOS6. The new API's are apparently supposed to be opt in however they seem to be enabled for all debug builds on simulator or device. To register for the new API calls throw something like this in your APP Delegates didFinishLoading method.
//Register for new API rotation calls
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"UIApplicationSupportedInterfaceOrientationsIsEnabled"];
At the heart of the rotation changes are two methods (theres a third but Im still figuring this out myself)
- (BOOL)shouldAutorotate
- (NSUInteger)supportedInterfaceOrientations
You need to override these methods in your windows rootViewController. This means you need to subclass UINavigationController or UITabBarController if either is your root controller (this seems bizarre to me, but Apple says Jump).
Now if all you want to do is keep your app in portrait implement the two methods and you're golden.
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Note that apple has further added to the confusion by adding interface orientation masks, ie. UIInterfaceOrientationMaskPortrait != UIInterfaceOrientationPortrait. If you return UIInterfaceOrientationPortrait instead the behaviour will be different. Also you can combine masks the same way you combine orientations so if you wanted to support both portrait orientations you could use.
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
That should work for forcing a portrait orientation. Now if you if you want do do something like allow a child controller to use a different orientation I have no clue.
A very simple way to handle autorotation in both iOS6 and iOS5 is to use supportedInterfaceOrientations & shouldAutorotateToInterfaceOrientation. There are some macros to make it just a line of code. UIInterfaceOrientationIsLandscape & UIInterfaceOrientationMaskLandscape.
I discovered UIInterfaceOrientationMaskLandscape & UIInterfaceOrientationMaskPortrait by auto-completion in xCode. It is not in the Apple docs about autorotation.
Add this code block to your root ViewController to force it to support only landscape mode.
//iOS6 code to support orientations
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscape);
}
//iOS5 code to support orientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return (UIInterfaceOrientationIsLandscape(orientation));
}
For iOS6 you can use the following to detect orientation:
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskAllButUpsideDown
UIInterfaceOrientationMaskAll
For iOS5 and below you can use the following to detect orientation:
UIInterfaceOrientationIsLandscape (A macro)
UIInterfaceOrientationIsPortrait
UIInterfaceOrientationIsLandscapeRight
UIInterfaceOrientationIsLandscapeLeft
UIInterfaceOrientationIsPortraitUpsideDown