I have an application with a tabbar in which i want to detect interface orientation.
I have set in info.plist the supported orientations.
This is my main interface declaration:
#interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
...
}
This is in the implementation of MyAppDelegate:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)
return NO;
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
if (interfaceOrientation == UIInterfaceOrientationPortrait)
return YES;
if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
return NO;
}
and in the necessary view where i'm trying to detect the orientation change, I'm calling these methods:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
and
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
I have set NSLog prints in these methods, an none of them is registering any changes. Not even in the shouldAutorotateToInterfaceOrientation method.
Can anyone please help me? what I'm i doing wrong?
Any help will be appreciated.
shouldAutorotateToInterfaceOrientation: is a method you have to implement in your view controllers, not in your app delegate. For a tab bar app to rotate, all child controllers of the tab bar controller have to return YES for the requested interface orientation.
Likewise, willRotateToInterfaceOrientation: and didRotateFromInterfaceOrientation: are to be implemented in the view controller, not in the view as you say you do.
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
method has to be implemented in your viewController, not in the appdelegate
Related
I have UITableViewController in which on one tab is UINavigationViewController. UINavigationController root view controller is UITableViewController, and when clicked on cell, UIViewController appears which has to be locked in Landscape.
I want every Controller to be locked in Portrait, except the mentioned UIViewController that must be locked in Portrait.
I have tried the following:
CustomTabBarController.m:
#import "CustomTabBarController.h"
#implementation CustomTabBarController
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// You do not need this method if you are not supporting earlier iOS Versions
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return [self.selectedViewController supportedInterfaceOrientations];
}
#end
CustomNavigationController.h:
#import "CustomNavigationController.h"
#implementation CustomNavigationController
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return YES;
}
#end
And in UIViewController that must be locked in to Landscape, I have put:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotate
{
return YES;
}
But it doesn't work, I can rotate it to Landscape and it will stay locked in Landscape, but I want it to appear automatically in Landscape.
Any suggestions?
I had a big problem in the past with UITabBarController not respecting my supported interface orientations of displayed view controllers.
I solved the problem by sub-classing UITabBarController and capturing whenever an item was selected. I'd then call down to the view controller myself, ask it what the supported orientations are and force a rotation myself if needed. I would also call down to the selected view controller on rotations to set/change my supported orientations.
I implemented the UITabBarDelegate and used didSelectItem to capture tab switches. I'm not sure if there is a better way to do it now.
Try to override method
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
Try with the method to block some orientations for a particular window:
– application:supportedInterfaceOrientationsForWindow:
I have two view controllers in a single project. However, I want one of the view controller's to autorotate, and the other to not.
If I set the master project setting as seen below:
Then, all view controllers autorotate, regardless of the following code in the view controller I do NOT want to autorotate:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
return NO;
}
However, if I set the master project settings as seen below, the view controller that I do not want to autorotate does not, but that also means neither can the one that I DO want to.
How must I integrate the master project (plist file) settings with those of the view controllers so that one view controller will auto-rotate while the other will not?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
was depreciated in iOS 6 so if that's what your project is running, that's why it's not working. What you need to do is implement:
- (NSUInteger)supportedInterfaceOrientations
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
The first one will tell the controller what orientation(s) it is allowed to use, and the second will tell it which one to use first. Note that the first method is only called if the method shouldAutorotate: returns YES.
These are the constants you can use for supportedInterfaceOrientations:
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortraitUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
Note that these only work on iOS 6.0.
Assume I am using tabbarController & iOS<6.0 try to use the following code solve your issue:
//In First View Controller
//BOOL activeStatus;
-(void)viewWillAppear:(BOOL)animated
{
activeStatus=YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
activeStatus=NO;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) && activeStatus==YES)
{
return YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//In Second View Controller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
I have a class, HomeView, as the rootViewController of a UINavigationController on my iPad application. Only landscape orientation is set in the info.plist file and HomeView does not implement shouldRotateToInterfaceOrientation:, however the view is rotating for both orientations while the simulator is rotating.
How do I only use the landscape orientations in my UIViewController?
If you do not implement shouldAutorotateToInterfaceOrientation: the runtime will call the method on the Super class, UIViewController.
Instead you should respond appropriately to the method with the orientations you want to rotate to:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
BOOL shouldAutorotate = NO;
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
shouldAutorotate = YES;
}
return shouldAutorotate;
}
Take a look at the UIInterfaceOrientation reference page and the UIViewController's shouldAutorotateToInterfaceOrientation: reference page for more information.
You shouldn't remove the shouldAutorotateToInterfaceOrientation method; that will not stop the interface from rotating, it will only not execute any code that you could have in that method. You should instead try something like this (assuming that you don't ever want the interface to rotate):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
our app got rejected, becouse the app does not rotate in upside down orientation.
so we have an tabbar App, adding this code to all tabs...
shouldAutorotateToInterfaceOrientation
makes no sense, add this code to a Appdelegate doesn't helps, what we do wrong?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
UITabbarcontroller is a subclass of UIViewcontroller. To solve you problem just subclass or add a category for you UITabbarcontroller implementing:
#interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
#end
#implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
If you want to make the tabbar only rotate to portrait and upside down just use th following code instead
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
Make sure that each UIViewController implements
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I have a button in my RootViewController ....
when i click on this button it will navigate to some otherViewController.
but i want that when i click on the Button otherViewController's Orientation should be Landscape
& when i back from this page(otherViewController) i should got again Portrait mode.
lots of Thanks for any help.
Plz help me i m new in Iphone so can't able to handle this.
Note in iOS 6 shouldAutorotateToInterfaceOrientation is deprecated. Instead you should use supportedInterfaceOrientations and return a UIInterfaceOrientationMask.
So for example:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
Here are all the UIInterfaceOrientationMask options you might return:
UIInterfaceOrientationMaskAll
UIInterfaceOrientationMaskAllButUpsideDown
UIInterfaceOrientationMaskLandscape
UIInterfaceOrientationMaskLandscapeLeft
UIInterfaceOrientationMaskLandscapeRight
UIInterfaceOrientationMaskPortrait
UIInterfaceOrientationMaskPortraitUpsideDown
You should also consider the preferredInterfaceOrientationForPresentation.
In general, orientation should only change from portrait to landscape (or vice versa) when the user rotates the device.
However, if you want your RootViewController to always present its view in portrait and your OtherViewController to always present itvs view in landscape, that's easy enough.
In your RootViewController's .h:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
In your OtherViewController's .h:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
Although this would probably be better as it would allow both landscape-left and landscape-right rotations:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
The only way I am aware this can be done is by presenting otherViewController modally.
See my answer here: UIViewController loads rotated to landscape but only supports portrait
Maybe it helps.
in viewWillAppear of your new view controller add
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscape]
and in viewWillAppear of your root viewController add
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
It will cause warning but it works.