shouldAutoRotate Method Not Called in iOS6

2019-02-10 23:38发布

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 ?

1条回答
时光不老,我们不散
2楼-- · 2019-02-11 00:05

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
查看更多
登录 后发表回答