I have a tab bar application in which i have 3 diffrent views each with there own view controller.
In the tab bar code i have this, to handle rotation.
#import "RotatingTabBarController.h"
#implementation RotatingTabBarController
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end
Then in the 2nd view controller that i want to rotate depending on device orientation i have:
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
And For the other two views that i do not want to rotate i have this method set.
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
The PROBLEM: so this works fine in view 1 and view 3 when u rotate the device they stay in portrait mode which is desired. When in view 2 i rotate to landscape, the view does as expected and rotates to landscape. BUT when click view 1 or view 3 tab while in lanscape mode in view 2, View 1 and View 3 are in landscape mode.
I can't figure out how to force them in portrait even if view 2 rotates to lanscape.
Any one know how to do this?
There's a big discussion[1] on this dating back from 2008 until now (look at comments down a few pages) -- summarily it seems like
application.statusBarOrientation = UIInterfaceOrientationLandscapeRight;
or
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
or
[application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
will let you force it to landscape -- you would want to do this when the user goes back to your landscapey view(s) programmatically.
[1] iPhone app in landscape mode, 2008 systems
Related
With iOS7, even though the app has the Device Orientation under General set to landscape and portrait. There are some screens in my app that I do NOT want to be able to rotate.
How can you pick and choose which views get rotated when the device turns?
I tried to use shouldAutorotateToInterfaceOrientation but I could not get it to work.
Anyone accomplished this with the new software?
try the following code in the views that you want it to be just landscape and change the landscape part to portrait in the views that you want it to be only portrait.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
Try overriding - (BOOL)shouldAutorotate and return NO.
I use story boards. In every view controler I want to rotate I included this method like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
in iOS 6 it rotates just fine to the proper views I have for landscape and portrait in my story boards.
in iOS 5 the parent view contoller rotates but the modals don't rotate while they are on the screen, they only rotate before they are on the screen but never during. So when a user changes orientation it remains the same. A parent will change while on screen but the modal will take on the orientation the parent had but will not change while on screen. How do I get my modal views to work like iOS 6 where they will rotate when on the screen. The modal view were created via the storyboard.
Edit*
When I do a popover to a modal, the modal doesn't rotate. How can I fix this?
Rotation is handled differently in iOS5 and iOS6. I use the below code to support both versions.
// iOS5
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
// iOS6
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
return YES;
}
shouldAutorotateToInterfaceOrientation: is deprecated in iOS6. You can read here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/UIViewController/shouldAutorotateToInterfaceOrientation:
In xcode 4 I am unable to lock the screen orientation to only portrait even though I have portrait selected only. How to I do this programmatically?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
this code works in a new blank project
The accepted answer did not work for me either. This did:
In the properties file for your app (YOURAPPNAME-Info.plist), located in the "supporting files" group, there is an array called "Supported interface orientations". Remove both landscape values from the array and your app will be locked in portrait orientation.
For each view controller (or just the parent), implement -shouldAutorotateToInterfaceOrientation: Set the default orientation to portrait and then do:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
Alternately, you can do:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
which will allow a rotation to portrait but not away from it.
I have a navigation controller and it goes through that in portrait. Then, at a certain point, I push to a view that displays a graph, which I want to only display in landscape, and not even be in portrait at all (this screws up how the view looks).
In this view, GraphViewController, I have the following method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
When the view first gets pushed, it appears in portrait mode. If I rotate the phone, the view will rotate into landscape also (not upside down portrait). But I want it to not even ever be in portrait mode, not even when it starts. I have verified that this method is getting called by adding a NSLog.
I saw these posts but could not get it to work. Thanks!!
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
I have a tab based application with 4 views associated with 4 tabs. I want all 3 views in portrait form and only one landscape form.
I have done these settings for that particular view:
Set the required value in Supported interface orientations of info.plist.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
}
In IB set the orientation attribute as landscape.
In -(void)viewDidLoad I have set self.view.frame=CGRectMake(0, 0, 480, 320);
After done all above setting, its still not coming in landscape mode.
If I understand your question correctly, and please correct me if I do not, then you want to have 3 views in portrait all of the time and one view in landscape all of the time. To accomplish this, set the orientation of the status bar for each viewController in viewWillAppear:
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}