Handling Rotation in iOS6

2019-01-23 21:36发布

I only want to support different Orientations on one View in my UINavigationController Stack. How can I do this?

It also has to work in iOS5.

3条回答
Deceive 欺骗
2楼-- · 2019-01-23 21:55

The solution does not work on iOS 6 (ok on iOS 5) when:

  • vc A is supporting only portrait orientation
  • vc B is supporting all orientations
  • we push vc B from vc A, rotate vc B (e.g. in landscape) and pop back to vc A. vc A orientation stays in landscape mode...
查看更多
在下西门庆
3楼-- · 2019-01-23 22:02

I recommend you to NOT create a category on UINavigationController to override those methods. Categories are not aimed to do that, and there is no warranty that your code is going to be loaded instead of Apple's one (even if actually that works). I advise you to create a subclass of UINavigationController, and override those methods in it.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-23 22:10

I've had a lot of trouble with how iOS6 handles Orientation, hopefully this is what you're looking for.

Create a category of UINavigationController and call it "UINavigationController+autoRotate".

Put this in your UINavigationController+autoRotate.h:

#import <UIKit/UIKit.h>

@interface UINavigationController (autoRotate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

@end

Put this in UINavigationController+autoRotate.m:

#import "UINavigationController+autoRotate.h"

@implementation UINavigationController (autoRotate)

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return [self.topViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

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


- (NSUInteger)supportedInterfaceOrientations
{
    if (![[self.viewControllers lastObject] isKindOfClass:NSClassFromString(@"ViewController")])
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    else
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
}

@end

For Views that you DO NOT want to rotate, add:

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return NO;
}

And for Views you DO want to rotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIDeviceOrientationPortraitUpsideDown);
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

In your App's delegate, add:

- (NSUInteger)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
查看更多
登录 后发表回答