Enable iPhone Interface Orientation - iphone

I'm trying to get my application to rotate the interface when the device itself is rotated, but I can't get it right.
I have added the supported interfaces in the plist info file and returned yes for the shouldRotateToInterfaceOrientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
} else {
return NO;
}
}
Is this how rotation is implemented?
Please help!

Maybe try editing the info.plist and also add your supported orientations there?
Select your project --> Select your target --> Info --> Supported interface orientations and click on the plus sign 4 times to support these orientations:
Item 0 Landscape (left home button)
Item 1 Landscape (right home button)
Item 2 Portrait (top home button)
Item 3 Portrait (bottom home button)
Hope it helps!

Make sure you've added shouldRotateToInterfaceOrientation to the view controllers that you want to support different orientations. It doesn't go in the app delegate.

Supporting orientation is straight-forward if you're using standard UI elements so, assuming that's the case, you're on the right track.
If you're using a UITabController, all the views must support the same orientations, otherwise it defaults to the minimum (e.g. Portrait), I believe.
Also, if you're using NIBs for your views, make sure you have 'Autoresize subviews' checked when configuring the view in Interface Builder.

if you are using UITabBarController, then you'll need to call it's subviews' shouldAutoratateToInterfaceOrientation.
suppose you have two tabs, please try to add the following two lines to method shouldAutorotateToInterfaceOrientation in the class that uses the UITabViewController.
[[tabBarController.viewControllers objectAtIndex:0] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
[[tabBarController.viewControllers objectAtIndex:1] shouldAutorotateToInterfaceOrientation:interfaceOrientation];
of course the "tabBarController" must be linked to the UITabBarController in your XIB file via IB.
Thanks,

Related

How to force my iPhone application to stay in a specific orientation

Yes, I know this question has been asked a lot of times, but I can not find anything that helps me any further.
Using a navigation controller with 3 viewcontrollers, I need to retain the data from previous screens so I do not use segues but like this:
// When button is pressed
- (IBAction)changeView:(id)sender {
NSLog(#"Skipped connection screen");
ViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SecondView"];
[self.navigationController pushViewController:vc animated:YES];
}
where SecondView is the identifier of the view controller that should appear. Since I only want the rotation to be at horizontal right, I add this snippet of code at the top of every .m file I have for my views:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
and in my project-Info.plist I have added Initial interface orientation = Landscape (right home button and in my project settings I have added support for this orientation only.
The problem is that when running on the iPhone, the orientation changes from landscape if I turn my phone either way. When I try to turn it back it just wont. I would like to make sure this application is never able to rotate away from landscape right.
Any suggestions? Thank you very much in advance.
I think if you will add below key to your .plist file then it will be fixed.
"Supported interface orientations"
Value of this key would be "Landscape (right home button)" or whatever you want so your application will support only that specified orientations.
Also add this code into every view controller.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
You should use the argument in your 'return' code:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Locking the orientation in an iPad app (plist or UIViewController?)

I'd like the app to work like it would be as if I locked the orientation manually. I'm trying to find how I can lock the orientation for an app. In the info.plist, I have this setting:
Supported interface orientations (iPad)
Item 0 Landscape (right home button)
Item 1 Landscape (left home button)
I thought that would be enough to keep my viewControllers from staying in landscape mode and not portrait. But it does not. Do I need to do
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
in ALL my viewControllers? Thanks!
All though implementing shouldAutorotateToInterfaceOrientation in all your view controllers will work, it is probably not the fastest or most practical way of doing what you are trying to accomplish.
If any of your view controllers in your hierarchy do not conform to the orientation change, then iOS will stop trying to rotate them. What this means is that only your root view controller needs to have implemented shouldAutorotateToInterfaceOrientation with only landscape orientations. Each view controller pushed or added will conform to that function.
I have had to do this in several of my apps and it was required for several reasons.
In the end and after a lot of testing, we determined that the condition has to be set on the info.plist AND on every viewController.
So make sure it is set on the plist and that every shouldAutorotateToInterfaceOrientationonly returns yes for the allowed orientation.
This is because the plist will help you with allowed LAUNCH orientations, but your app could still rotate afterwards, specially when using modal views.
You can download one of my free apps that does thins on iPad: http://itunes.apple.com/mx/app/hoteles-city/id471505865?mt=8
Yes you do.
I have a different solution however. In every UIViewController, I use:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
} else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
} else {
return NO;
}
}

Xcode 4 component orientation problem

I'm using xCode 4 for my project which should work both on iPad and iPhone. So, i have created Universal windows based application. I want my application starts into landscape mode so i added following:
in method:
shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
I set interfaceOrientation == UIInterfaceOrientationLandscapeRight
and added the key into application.plist:
Initial Interface Orientation and set it on Landscape (right home button).
Now, the application did start in landscape mode, however, every component in it (button, label) remains rotated 90 degrees (counterclockwise).
When i load xib, Window orientation is set into Portrait and dropdown is disabled.
anybody has idea what i'm doing wrong?
shouldAutorotateToInterfaceOrientation isn't asking you to set the variable (and will have no way of reading the result even if you do), it's a query as to whether the nominated orientation is acceptable. So you want:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight) return YES;
return NO;
}
/* or: return interfaceOrientation == UIInterfaceOrientationLandscapeRight; */
The XIB is a separate issue; neither Interface Builder nor its replacement parts in Xcode 4 can see what you're doing in your code, so they shouldn't just decline to let you design in landscape mode. However, it's the view that is linked to the view member of the relevant UIViewController that you want to set the orientation on, not the window.

iPhone - Landscape Only App

I am trying to make an app that will only be viewed in Landscape. I have looked up some tutorials (albeit older ones) and have done the following:
-set up the info.plist to include a key for uiinterfaceorientation
-in the main view controller I have set the frame to be 480 x 320
Now, the first screen loads up ok. Everything is where it should be and whatnot. However, if I click a button that is set to present a modal view controller nothing happens. Everything is linked and coded correctly but nothing happens when I press the button. Am I doing something wrong with trying to force landscape?
At it's basic, this question is a how do you effectively make an app that will only be in landscape mode? Thanks for any help.
You should only uncomment this method I show you below:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

iPad launch orientation not detected

I have an iPad app that works correctly except for an odd issue during launch. I've read several questions & answers regarding orientation, but this still has me stumped.
The root view controller is a UITabBarController with 3 tabs. Two of the tabs are have custom view controllers (one based off of UIViewController, the other off of UITableViewController) and both suffer from this launch orientation problem. The third tab is a custom UITableViewController that's embedded in a UINavigationController.
OK, here's the problem. If I start the app in Portrait orientation, everything works great. If I start it in Landscape orientation, the 3rd tab works perfectly. However, the first 2 tabs come up in Portrait orientation, even though:
The status bar orientation correctly shows as landscape (spread across the screen).
The Tab Bar view correctly shows as landscape with the tabs centered.
All views return YES for shouldAutorotateToInterfaceOrientation for all orientations.
If I call [self interfaceOrientation] or [[UIApplication sharedApplication] statusBarOrientation] in the view controller's viewWillAppear, then the 3rd tab's view controller reports 3 (landscape) but the first two view controllers report 1 (portrait) even though the status bar is clearly landscape!
If I rotate the iPad to portrait and back to landscape, then all 3 tabs' views rotate correctly (and the methods above return 3, as expected).
Also, if I tap on any other tab and then back on tab #1 or #2, then they will now rotate correctly, even without rotating the iPad itself!
What am I missing?
You have to add the supportedDeviceOrientations to your "myApp.plist" .
Click on this list, add the key "Supported interface orientations" and add the supported interface orientations. This solved the problem for me.
For further informationen follow this link and go to the section "The Application Bundle": http://developer.apple.com/iphone/library/documentation/General/Conceptual/iPadProgrammingGuide/CoreApplication/CoreApplication.html
I finally found my answer: I just forgot this in my LoadingController.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I have found that the device orientation starts out with nothing. And should return YES for Unknown. This will allow it to orient the device with the correct launch orientation.
Here is the code I used to propigate this message up to the legacy messages.
- (BOOL)shouldAutorotate{
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationUnknown) return YES;
BOOL result = [self shouldAutorotateToInterfaceOrientation:orientation];
return result;
}
notice I return YES if orientation == UIDeviceOrientationUnknown. This corrected my loading problem.
The solution is to add a key
UISupportedInterfaceOrientation
to you Info.plist with an array of strings specifying the suppored interface orientations at launch time, these are
UIInterfaceOrientationPortrait
UIInterfaceOrientationPortraitUpsideDown
UIInterfaceOrientationLandscapeLeft
UIInterfaceOrientationLandscapeRight
However, there is the follwing issue which may lead to confusion: At least with SDK 3.2 and iPad Simulator from XCode 3.2.4 I found that (at least some) Info.plist settings appeared to be cached and/or are not updated when installing the app. That is, adding the key above and installing and launching the app in the simulator had no effect. However, deleting the app from the simulator fixed the problem an the newly installed app behaved as specified.
In your app delegate's applicationDidFinishLaunchingWithOptions: method, after you add your view controller's view to the window, add the following:
[myViewController viewDidLoad];
If necessary, this will trigger a call to the shouldAutorotateToInterfaceOrientation: method.
just try this
- (BOOL)shouldAutorotateToInterfaceOrientation: UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown);<br>
}