I have a UIView thats inside a navigation controller, I am trying to prevent this view from going into Landscape however the method I am trying to use never fires.. code is below any help would be greatly appreciated..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
return NO;
}
else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
{
return NO;
}
return NO;
}
You should set return NO; for the parent navigation controller or on a UIViewController, not a UIView.
Also, this works just the same with less code:
iOS 5.x
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
iOS 6
As of iOS 6, shouldAutorotateToInterfaceOrientation: is deprecated. If the view controller does not override the supportedInterfaceOrientations method, UIKit obtains the default rotations from the app delegate or the app’s Info.plist file.
You will need to use:
- (NSUInteger)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
Another option is modifying Deployment Info.
This method needs to be implemented in a UIViewController instance, not a UIView.
Also, you are in fact returning NO for all orientations, which is incorrect. You need to return YES for at least one orientation (most commonly UIInterfaceOrientationPortrait).
Related
I have a strange problem with UIInterfaceOrientation. In my project there are many different views, some of them should rotate in landscape mode, and some of them should not. The problem is that all the view which were not created using Storyboard and in which only the UIInterfaceOrientation portrait is enabled this works fine and the view does not rotate, instead all the views created using the Storyboard, even if the UIInterfaceOrientation landscape mode was disabled they keep rotating. In my Xcode project setting those checks are enabled and I cannot change them:
How can I completely disable the device rotation in all the different views? [Storyboard or not].
This is the code I use to disable the device orientation in all the storyboard view controller, but it does not work:
- (BOOL)shouldAutorotate {
return NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
Try this code maybe it will work for you.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientationMask)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationMaskPortrait);
}
The solution was to assign a UINavigationController class to the UINavigationController in the Storyboard file and to place this code in his .m file:
- (BOOL)shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
Please, check if in your project .plist there are more than one item for orientation or something strange. I sometimes have found that orientation has different values in plist or duplicated keys.
Hope it helps
My entire app is locked in portrait orientation but when a video is played I wanna allow all orientations just for the video playback.
Storyboard:
TabBarController --> NavigationController --> MyVideosController --> MyVideoPlayerController
This is one of many I've tried:
Allow One View to Support Multiple Orientations While Others Do Not iPhone
The problem is that I never even reach into this method in my MyVideoPlayerController.m:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
In my experience just about every suggestion you will find on here won't work for you. What you are going to want to do is (if you are using storyboards) add another navigation controller and a view controller. You will push the 2nd navigation controller modally and lock it to the orientation you desire. This way your video player can "pop up" and be in the orientation(s) you want.
Its really annoying.
An alternative (which I don't really suggest) is that you can turn on AutoLayout and perhaps try the IOS6 version.
For iOS-6 , I have done this , it is running greatly
(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
try this code, add it to your class it's working for me:
#pragma mark - Orientation
- (BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
ADDED:
You can access this project on github
ios6rotations
Sorry guys for asking the question about screen rotation in iOS 6 but this is really a pain in the ass..and I still can't understand it completely - for some reason it behaves differently under certain circumstances.
I have the following simple hierarchy of views in my test app:
What I'm trying to achieve is - to keep blue controller in landscape only and red one is only in portrait.
I have a subclass of UINavigationController with such code inside:
#implementation CustomNavController
- (BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
In my blue controller I implemented this:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
And in red controller this:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Now I have the following behavior:
App started in landscape (OK)
When I press the button my red controller pushed in landscape too (this is not ok because it must be shown in Portrait)
It successfully rotates to portrait but not backward to landscape
If I leave the red controller in Portrait mode my blue controller (which is restricted to landscape) shows in Portrait mode.
P.S.
All my rotation methods(posted above) are getting called normally.(by the way why do these methods getting called so many times per screen transition - 5-6 times)
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation does not getting called with pushing
All(except portraitUpsideDown) orientations are included in plist.
The question is - how to force rotation to supported orientation in each controller?
I suggest you to post here (as answers) any 100% working code to handle rotations in ios6 (for example if you have some for iPad with SplitController) - I'll keep this question in favorites to have all in one place when I need to handle some specific situations. Cheers!
ADDED:
Please do not post this as answer from landscape to portrait I hope that there'
s more elegant way to do it.
Using -[UIDevice setOrientation:] is a private API, and will get your application rejected. See this question.
What you ask is not possible using public API and is also not recommended from HIG standpoint. What is supported and you should implement, is modal presentation of the different view controllers with different supported interface orientation. This is why the default implementation of UINavigationController is to always rotate; it assumes all view controllers have the same supported interface orientations.
Take for example video playback on iPhone. Open the video apps (that comes with iOS). The root view controller only supports portrait orientation. However, start a video, and a modal view controller pops up which only supports landscape interface orientations. This seems exactly the behavior you wish to achieve.
This is why preferredInterfaceOrientationForPresentation is not called. preferredInterfaceOrientationForPresentation only gets called when using presentViewController:animated:.
A small gotcha, if you require a navigation bar in each stage of your scene, you will need to enclose each modal view controller with a navigation controller. You can then pass the required data in prepareForSegue: by accessing topViewController of the navigation controller object in the segue.
Here is an example project which behaves correctly according to your requirements (or at least will give you ideas how to implement):
http://www.mediafire.com/?zw3qesn8w4v66hy
My two cents worth.
You can present an empty transparent modal view quickly then dismiss it, maybe on ViewDidLoad: or viewWillAppear: on your ViewController and ViewControllerSecond class as a quick workaround.
Also, in storyboard, you can set ViewController class orientation to landscape visually.
use this line for programmatically change orientation... work 100%
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
and also when you add this line at that time one warning appear and for remove this warning just add bellow code on you implementation file.. at the top.
#interface UIDevice (MyPrivateNameThatAppleWouldNeverUseGoesHere)
- (void) setOrientation:(UIInterfaceOrientation)orientation;
#end
and after that in bellow method just write this code if required..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return your supported orientations
if (currentMainView==blueOne) {
return toInterfaceOrientation== UIInterfaceOrientationPortrait;
}
}
I have a similar situation in one of my apps (although do note that I am not using UINavigationController).
Change the shouldAutorotate methods in both of your viewControllers:
//in blue (landscape only)
-(BOOL)shouldAutorotate{
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
return YES;
}
else {
return NO;
}
}
//in red (portrait only)
-(BOOL)shouldAutorotate{
if (self.interfaceOrientation == UIInterfaceOrientationPortrait) {
//note that UIInterfaceOrientationIsPortrait(self.interfaceOrientation) will return yes for UIInterfaceOrientationPortraitUpsideDown
return YES;
}
else {
return NO;
}
}
Keep the supportedInterfaceOrientations methods the same.
#pragma mark- Orientation Delegate Method:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{ Orientation = [UIApplication sharedApplication].statusBarOrientation;
if (Orientation == UIInterfaceOrientationLandscapeLeft || Orientation == UIInterfaceOrientationLandscapeRight)
{
// self.scrollView.contentOffset = CGPointMake(self.view.bounds.size.width,1200);
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(768, 2150)];
}else if (Orientation == UIInterfaceOrientationPortrait || Orientation == UIInterfaceOrientationPortraitUpsideDown)
{
[scrollView setScrollEnabled:YES];
[scrollView setContentSize:CGSizeMake(768, 1750)];
}
}
In order to use navigation with orientation together, you should take a bunch of viewcontrollers like an array.
After that checkout following methods,
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
this changes in your methods will help you a lot.
Enjoy Programming!
I'm working on a storyboard app and the rotation works on iOS6 devices but when I run it on my iOS5 device it won't rotate. I have a View Controller embedded in a Navigation Controller (so the Navigation Controller is the root view controller of the window) and I have the following methods overridden:
- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutoRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight) || (interfaceOrientation ==UIInterfaceOrientationPortrait)){
return YES;
} else {
return NO;
}
}
I also have the 3 orientations allowed in my plist, and my deployment target is iOS5 with the base SDK iOS6 and autolayout is turned off, and ideas why this isnt working?
Thank you!
Your comment caused me to look closer. It's because you have a capital 'R' in autorotation.
Change your method name to:
shouldAutorotateToInterfaceOrientation:
I want to support iOS 6 rotation. Trouble is, I've been looking through a lot of documentation and stack overflow questions but have not found any even slightly in depth solutions. I've only seen that I should add these two methods to my view controller classes - however, if I'm not mistaken, they do not operate in the same way as the pre iOS 6 methods:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll; // use what is appropriate for you.
}
My app currently rotates in pre-iOS6 using the following code. Note that I use the interface orientation parameters to determine whether or not I'm going to push my view Controller. How do I implement this in the iOS 6 rotation delegates?
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if ( self.tabBarController.view.subviews.count >= 2 )
{
UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];
if(toOrientation != UIInterfaceOrientationLandscapeLeft && toOrientation != UIInterfaceOrientationLandscapeRight)
{
CUSTOM_DEBUG_LOG("\n\nRotated back to Portrait");
tabBar.hidden = FALSE;
}
}
}
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
CUSTOM_DEBUG_LOG("\nView going landscape");
ScrollViewController *s = [[ScrollViewController alloc] initWithNibName:#"ScrollViewController" bundle:nil];
[self.navigationController pushViewController:s animated:NO];
[s release];
self.tabBarController.tabBar.hidden = YES;
self.navigationController.navigationBar.hidden = YES;
}
}
Checkout this and this SO discussion.
[EDIT]
Yes the methods you mentioned aren't deprecated in iOS 6.0 and they will continue working. It's just the way Auto Rotation works have been changed. So far it was view controllers responsibility to decide whether they rotate or not but now RootViewController will decide whether their children should rotate or not. If you don't have rootviewcontroller setup then you have to add it to window and then put shouldAutoRotate and supportedInterfaceOrientations methods in the rootviewcontroller.
Parent Views now handle rotation in iOS 6. Subclass your nav controllers and add a bool
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait; // your rotation here
}
I may not have implemented the iOS6 rotation code correctly when I first posted the question.
I incorrectly thought that the willAnimateRotationToInterfaceOrientation function was deprecated in iOS6, leading me to believe that there was a new iOS rotation delegate with an orientation parameter. Turns out this is not the case, so my app sort of works.
The code I plugged into my app was just this:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight);
}
If you are using a UINavigationController, override shouldAutomaticallyForwardRotationMethods = YES property.
Then like Mark S said, also override shouldAutorotate and supportedInterfaceOrientations for the children VCs.