3

I have a UIViewController detail view which is pushed from a UITableView in a UINavigationController. In the UIViewController I add a number of subviews (e.g a UITextView, UIImageView).

In iOS5 I used this code to stop autorotation if my picture view was enlarged :

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (scrollView.isZoomed) {
    return NO;
}
else {
    return YES;
}

}

I am trying to achieve the same thing under iOS6 using :

- (BOOL)shouldAutorotate {
return FALSE;
}

However this method is never called and the app continues rotating.

Can anyone help ?

GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113

1 Answers1

3

If you have a Navigation Controller managing these views, the shouldAutorotate method won't be called. You would have to subclass UINavigationController and override methods shouldAutorotate and supportedIntervalOrientations.

From the docs:

Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate

Edit-----

As mentioned below by Lomax, subclassing UINavigationController is discouraged by Apple. You should try a category instead (this SO question explains it well):

@implementation UINavigationController 
-(BOOL)shouldAutorotate
{
    // your code
}

-(NSUInteger)supportedInterfaceOrientations
{
    (...)
}

@end
Community
  • 1
  • 1
alemangui
  • 3,571
  • 22
  • 33
  • 1
    From UINavigationController documentation: This class is not intended for subclassing --> if you subclass your app will probably be rejected. – LombaX Nov 27 '12 at 16:51
  • You are right, it is better to use categories. I'll edit my answer – alemangui Nov 27 '12 at 16:52
  • Thanks - how do I use a category - is it just a custom class ? – GuybrushThreepwood Nov 27 '12 at 16:57
  • Check the SO link in the answer. Also, this is a good tutorial on categories: http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/ – alemangui Nov 27 '12 at 17:00
  • 4
    UINavigation Documentation as of iOS 6..."This class is generally used as-is but may be subclassed in iOS 6 and later." So subclassing will not get your app rejected. [link](https://developer.apple.com/library/ios/documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html) – Gallonallen Sep 17 '13 at 17:21
  • 1
    Categories are used in objective c for adding new methods to existing classes. Using category for method overriding is bad practice. SUBCALSS UINavigationController in iOS >= 6.0. – dimaxyu Dec 03 '13 at 05:41