0

In my iphone app,I have marked supported orientations as below screen shot.

enter image description here

But I want to prevent some view on auto rotation,for that I am using the below code(ios6)

EDIT

-(BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation
{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}


- (BOOL)shouldAutorotate
{
return NO;
 }

but the view still rotates,Please help me to solve the issue?

Community
  • 1
  • 1
  • - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } – iPatel Feb 26 '13 at 05:07
  • What version of iOS? The `shouldAutorotateToInterfaceOrientation:` isn't used under iOS 6.0+. For that you need the `supportedInterfaceOrientations` method. If you support 5.x and 6.x then implement both methods. – rmaddy Feb 26 '13 at 05:10
  • http://stackoverflow.com/a/12995064/1339473 same issue as you.. – QuokMoon Feb 26 '13 at 05:12

6 Answers6

4

iOS 6.0 introduces new rotation methods and doesn't call shouldAutorotateToInterfaceOrientation:.

You need to implement these methods to support both pre-iOS 6.0 and iOS 6.0+ :

// Do as many checks as you want to allow for other orientations here for pre-iOS 6

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

// You can return YES here as this still checks supportedInterfaceOrientations
// before rotating, or you can return NO to 'lock' the view in whatever it's in... 
// Making sure to return the appropriate value within supportedInterfaceOrientations

- (BOOL)shouldAutorotate
{
    return YES;
}

// return supported orientations, bitwised OR-ed together

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

EDIT- If your UIViewController is within a UINavigationController's hierarchy

If your view controller is within a UINavigationController's view hierarchy, the rotation methods are actually called on the navigation controller (not the view controller)... this was implemented funny by Apple in my opinion, but here's what you can do to allow the view controller to respond to these methods instead-

Create a category on UINavigationController with the methods as follows:

// UINavigationController+Additions.h file

@interface UINavigationController (Additions)
@end




// UINavigationController+Additions.m file

#import "UINavigationController+Additions.h"

@implementation UINavigationController (Additions)

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    UIViewController *viewController = [self topViewController];

    if ([viewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
    {
        return [viewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }

    return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController *viewController = [self topViewController];

    if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [viewController supportedInterfaceOrientations];
    }

    return [super supportedInterfaceOrientations];
}

@end

EDIT 2 - If your UIViewController is within a UITabBarController's tabs

There's the same issue if your view controller is within a UITabBarController's tabs.

once again, create a category on UITabBarController to allow the view controller to respond instead:

// UITabBarController+Additions.h file

@interface UITabBarController (Additions)
@end

// UITabBarController+Additions.m file

#import "UITabBarController+Additions.h"

@implementation UITabBarController (Additions)

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    UIViewController *selectedViewController = [self selectedViewController];

    if ([selectedViewController respondsToSelector:@selector(shouldAutorotateToInterfaceOrientation:)])
    {
    return [selectedViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }

    return [super shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController *selectedViewController = [self selectedViewController];

    if ([selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [selectedViewController supportedInterfaceOrientations];
    }

    return [super supportedInterfaceOrientations];
}

@end

EDIT 3

Here's also some links about Objective-C Categories:

http://macdevelopertips.com/objective-c/objective-c-categories.html

http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

JRG-Developer
  • 12,454
  • 8
  • 55
  • 81
  • This code rotates my view...:( –  Feb 26 '13 at 05:18
  • What orientation does this view start out in...something other than Portrait? – JRG-Developer Feb 26 '13 at 05:20
  • If you just want to **lock** the view to whatever it's in, have `shouldAutorotate` return NO. – JRG-Developer Feb 26 '13 at 05:21
  • no..Its starting with portrait itself –  Feb 26 '13 at 05:25
  • I gave it return NO; but still the view getting rotated –  Feb 26 '13 at 05:26
  • This method works in our projects... Can you copy and paste all of your rotation methods as an edit to your original post? – JRG-Developer Feb 26 '13 at 05:33
  • As another thought, is this view controller within a `UINavigationController` hierarchy or within a `UITabBarController` tabs? – JRG-Developer Feb 26 '13 at 05:36
  • tabbar controller...(I have edited the code) –  Feb 26 '13 at 05:38
  • Please see the edited answer... you have to create a category on `UITabBarController` to ask the view controller what to do... otherwise, the tab bar controller will call its own rotation methods instead (which will return the values you specified in supported interface orientations by default)... – JRG-Developer Feb 26 '13 at 05:46
  • but...actually is it necessary for rotating the movie player inside a tabbar controller ? –  Feb 26 '13 at 05:50
  • Yes, unfortunately Apple implemented it this way... otherwise, if you don't have said category, the tab bar controller's rotation methods will be called (not the movie player's methods)... this was poorly implemented by Apple, in my opinion... – JRG-Developer Feb 26 '13 at 05:52
  • So I have to implement all this codes...mm I am unfamiliar with categories..do have any sample code(or links) for this kind of rotation –  Feb 26 '13 at 05:57
  • All of the code is within the body of the answer... to create a category in general though... (1) Select `File > New > File`, (2) choose `Cocoa Touch` on Left sidebar, (3) select `Objective-C Category`, (4) name as "Additions" or something descriptive for the `Category` field and enter "UITabBarController" for the `Category On` Field, and (5) copy and paste the relevant code from `UITabBarController+Additions` in the answer into the header (.h) and implementation (.m) files. – JRG-Developer Feb 26 '13 at 06:00
  • Thanks JRG..for this much of help,thank you so much I am trying this.. –  Feb 26 '13 at 06:04
  • hey bro..I have created the categories and added the code as you said...should I do anything in view controller too ?? –  Feb 26 '13 at 06:18
  • Make sure that you have the rotation methods implemented on the view controller, too. The tab bar controller will call them to see what to do. – JRG-Developer Feb 26 '13 at 06:20
  • I have added below code in my view controller...but it still rotates - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate { return NO; } –  Feb 26 '13 at 06:21
  • Try also adding `- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }` to the view controller's methods (the tab bar controller category always returns YES to `shouldAutorotate`, and it references the `supportedInterfaceOrientations` of the view controller to see if it should actually rotate. – JRG-Developer Feb 26 '13 at 06:26
0

shouldAutorotateToInterfaceOrientation is deprecated in iOS 6.

Check the answers of this question for more details.

Community
  • 1
  • 1
sapi
  • 9,944
  • 8
  • 41
  • 71
0

If you want to fix orientation for particular view try this,

-(BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait; //IOS_6
}

EDIT

As you are having tab bar you need to have work around for that. Please look at the link. Hope that can help.

  1. Link 1
  2. Link 2
Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
0

As far as I can see,You'd better also write antoher two methods as follows

- (BOOL)shouldAutorotate
{
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
alla
  • 31
  • 3
0

Do you really want your orientation Upside Down or just Portrait? i built my app for portrait orientation,try this code(for portrait orientation,modify if other)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if(interfaceOrientation == UIInterfaceOrientationPortrait)
    return YES;

return NO;
}
EarlySun
  • 185
  • 2
  • 12
  • Won't work in iOS 6.0+... you've got to implement the new rotation methods too based on his `supported interface orientations` selected. – JRG-Developer Feb 26 '13 at 05:17
0

When a UINavigationController or UITabBarController is involved, subclass the UINavigationController/UITabBarController and overriding supportedInterfaceOrientations.

 #import "UINavigationController+Orientation.h"

 @implementation UINavigationController (Orientation)

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

@end  

// For UITabBarController

#import "UITabBarController+Orientation.h"

@implementation UITabBarController (Orientation)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController  shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}

@end

Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate.
How to sub class
1. Add a new file (Objective c- category under cocoa touch)
2. Category : Orientation Category On: UINavigationController
3. Add the above code to UINavigationController+Orientation.m

D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
  • Ok..I have added the class for UINavigationController And what do I do in view controllers –  Feb 27 '13 at 10:20