iOS8 Modal ViewController不会触发shouldAutoRotate。

时间:2015-05-27 14:34:08

标签: objective-c ios8 shouldautorotate

我有一个parentViewController,它使用Delegate UITableViewController调用模态ViewController。问题是我没有导航堆栈来从父级的shouldAUtoRotate获取值。

由于shouldAutorotate从未触发,因此从主控制器获取它,返回NO并且无法更改。有没有办法操纵模态控制器以将正确的shouldAutorotate设置为YES?

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self.pv dismiss:YES];
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
            //[self fullScreenAction:nil];
        } else {
//            [self exitFullScreenAction:nil];
        }
    }
}
-(BOOL)shouldAutorotate
{
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        return UIInterfaceOrientationMaskAll;
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

//ios >=6
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ||
    (interfaceOrientation == UIInterfaceOrientationPortrait) ||
    (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
    (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

1 个答案:

答案 0 :(得分:0)

我设法解决了我的问题,虽然不是最好的解决方案,也许有些会有用。

所以我创建了一个新的Category Controller,以便用它来手动设置Rotation调用。

.m

#import "UINavigationController+AutoRotate.h"

@implementation UINavigationController (AutoRotate)

- (BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationMaskAll;
}
@end

和.h

#import <UIKit/UIKit.h>

@interface UINavigationController (AutoRotate)

- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

@end

然后Modal在没有导航控制器的情况下正常工作。