Autorotation not working ios6.1 - iphone

Autorotation is not working in ios6.1. But following code is working in other app.
code:
#implementation UINavigationController (RotationIn_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
- (BOOL)shouldAutorotate
{
//returns true if want to allow orientation change
return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
//decide number of origination tob supported by Viewcontroller.
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//from here you Should try to Preferred orientation for ViewController
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationPortrait;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration
{
}

ios6 uses mainly the plist options for autorotation. Try to setup the supported orientations fields.

Have you tried to modify the plist?

Please check your project targets summery is like below

Related

UIImagePickerController not opening in iOS 6.0

I am trying to open a UIImagePickerController object in my application using this code:
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imgPicker.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:self.imgPicker animated:YES completion:nil];
But then the application crashes and displays the following error:
Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason:
'preferredInterfaceOrientationForPresentation must return a supported interface orientation!'
I also added following methods in my ViewController class
- (BOOL) shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationMaskPortrait;
}
So, what else should I do to present this UIImagePickerController?
This seems to be because the choose-photo VC doesn't implement the new orientation methods. To fix this, make sure for root view controller does implement them, even when the top VC doesn't.
In code:
#implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate {
return [self.topViewController shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations {
UIInterfaceOrientationMask topControllerOrientationMask = [self.topViewController supportedInterfaceOrientations];
return topControllerOrientationMask ? topControllerOrientationMask : UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
UIInterfaceOrientation topControllerOrientation = [self.topViewController preferredInterfaceOrientationForPresentation];
return topControllerOrientation ? topControllerOrientation : UIInterfaceOrientationPortrait;
}
#end
About the crash problem you can read Here
Also shouldAutorotateToInterfaceOrientation must return not UIInterfaceOrientationMaskPortrait, but UIInterfaceOrientationPortrait,
about the difference you can read Here
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationPortrait;
}
I think your trying to launch image picker your app on iPad. If it is so, you cannot launch picker this type on iPad, it works only on iPhone. For using ImagePicker on iPad, launch imagePicker using UIPopOver.
I have same problem few days ago. I found this following trick.
Put this code in your
AppDelegate.m
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAll;
}
And also put this code in your UIImagePickerViewController class.
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
Because in iOS-6 UIImagePickerController must be follow portrait orientation.

Supported Interface Orientation and shouldAutorotateToInterfaceOrientation

In my project settings i made Supported Interface Orientation as both landscape.
Should i still implement below in each view controller?
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
iOS < 6.x
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation // Deprecated in iOS 6.x
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
iOS > 6.x
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
hope this will help.
ya its better to implement and it would be better if you implement it in info.plist the orientation to landscape
for ios>6
- (BOOL) shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}

Alternate landscape triggered at unwanted view

I have a view controller that contains an image and a tableview. From this view controller I connected a segue to a landscape view that contains the image in full screen (same idea used when you turn sideways the Stocks app from Apple to see the graph in full screen).
This segue is called by the following method:
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
[self performSegueWithIdentifier: #"toGraph" sender: self];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Besides this, when the iphone is at portrait orientation, I can also drill-down the tableview a couple more levels. The problem is the following: at those consequent levels, the segue to the landscape view still gets triggered when I turn the orientation to landscape!... what can I do so that this does not happen? I am only interested in going to landscape mode from the first view which contains the image.
Thank You in advance!
I don't know for sure if I got your issue right.. but if you mean with "drill-down the table view" to go deeper in a navigation controller hierachy, you can try the following..
That's what I did in a (I think) similar situation:
AppDelegate:
in .h:
#property (nonatomic) BOOL shouldAutorotate;
in .m:
// in didFinishLaunchingWithOptions:
self.shouldAutorotate = NO;
// still in .m file
// Autorotation handling
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return self.shouldAutorotate ?
UIInterfaceOrientationMaskAllButUpsideDown :
UIInterfaceOrientationMaskPortrait;
}
Navigation Controller presenting Portrait Controller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
if (self.selectedViewController)
return [self.selectedViewController supportedInterfaceOrientations];
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return YES;
}
Portrait View Controller (here's also a very similar segue handling that you have):
in viewWillAppear:
[(AppDelegate *)[[UIApplication sharedApplication] delegate] setShouldAutorotate:YES];
rotation handling:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Landscape View Controller (probably your full screen image):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
Deeper in the navigation controller hierachy (where only portrait is wanted):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Looks somehow complicated, but that was the only way, I managed to get those rotation things working properly in both iOS5 and 6.

iOS 6 AutoRotate In UiNavigationController

I have UiNavigationController in my app.
I want that only one screen will be able to rotate so i put in this class :
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
-(BOOL)shouldAutorotate {
return YES;
}
-(NSInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
but the problem that happen is in ecery screen the app the rotation happens. how i can disable it?
For iOS 6, I am using the following code in my app, which allows you to specify rotation for each viewcontroller individually:
AppDelegate.m -
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{NSUInteger orientations =UIInterfaceOrientationMaskAllButUpsideDown;
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
ViewController.m -
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
Credits for the code originially I believe go to the Ray Wenderlich "iOS 6 by Tutorials" book. Ray Wenderlich website
Use the below code in the class you want to autorotate only:
#interface UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (NSUInteger)supportedInterfaceOrientations;
- (BOOL)shouldAutorotate;
#end
#implementation UITabBarController (rotation)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return YES;
}
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
if ([self.selectedViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *navController = (UINavigationController *) self.selectedViewController;
if ([[navController visibleViewController] isKindOfClass:[CLASS_NAME_FOR_ROTATION class]])
return YES;
}
return NO;
}
#end
Use this code in the parent View Controller of the class (i.e. the just previous class on the stack of navigation controller) that is to be rotated.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
In your appDelegate add the this code
#property(nonatomic,assign)BOOL shouldRotate;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
shouldRotate=NO;
}
-(void)shouldAutoRotate:(BOOL)rotate
{
self.shouldRotate=rotate;
}
And in your rootview controller add this code
#import "AppDelegate.h"
#define myAppDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate]
- (NSUInteger)supportedInterfaceOrientations
{
if(![myAppDelegate shouldRotate])
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAllButUpsideDown;
}
After that add this code in viewcontroller.m which is you want to rotate
- (void)viewDidLoad
{
[super viewDidLoad];
[myAppDelegate shouldAutoRotate:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[myAppDelegate shouldAutoRotate:NO];
}
I have done this for one of the my projects( IOS 7).It works for me perfectly.
You can create a category to override the methods in navigationController to support all classes.
#implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
If you want to restrict the rotation in different controllers then override the supportedInterfaceOrientations and shouldAutorote in respective viewcontrollers changing the return value as required.
-(NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}

i want to know device orientation when the view will appear IOS 6

I want to know device orientation when the view will appear.
Previously it was possible using the shouldAutorotateToInterfaceOrientation method but in IOS 6 it is deprecated.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (interfaceOrientation==UIInterfaceOrientationPortrait ||interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown ) {
DashBtn.hidden=NO;
}
else
{
DashBtn.hidden=YES;
if (self.popoverControllerMain) {
[self.popoverControllerMain dismissPopoverAnimated:YES];
}
}
return YES;
}
I checked all the post
i.e making rootviewcontroller and
- (BOOL)shouldAutorotate {
return NO;
}// this method is not called
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Create category of UINavigationController and add below code #implementation UINavigationController (Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
Try using
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
instead of
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
refer http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html for details.
The best option to know the device orientation is
[[UIApplication sharedApplication] statusBarOrientation]