iOS横竖屏小记

一、横竖屏有控制优先级,一旦优先级高的关闭了横竖屏配置,优先级低的无论如何配置都无法做到横竖屏。

对于限于VC范围来讲优先级最高的是当前的window的rootViewController,而往往我们的项目结构是容器视图控制器控制VC,tabBarController控制navigationController之后是VC,而横竖屏控制的优先级也是跟你的项目架构一样。appDelegate>>rootViewController>>ViewController

解决方法:1.利用模态跳转到VC;

                   2.配置根视图:

- (BOOL)shouldAutorotate {

return [[self.viewControllers lastObject] shouldAutorotate];

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

return [[self.viewControllers lastObject] supportedInterfaceOrientations];

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];

}

-(BOOL)shouldAutorotate {

if ([[self.viewControllers lastObject]isKindOfClass:[NewViewController class]]) {

return YES;

}

return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

if ([[self.viewControllers lastObject]isKindOfClass:[NewViewController class]]) {

return UIInterfaceOrientationMaskLandscapeLeft;

}

return UIInterfaceOrientationMaskPortrait;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

if ([[self.viewControllers lastObject]isKindOfClass:[NewViewController class]]) {

return UIInterfaceOrientationLandscapeLeft;

}

return UIInterfaceOrientationPortrait;

}

强制横屏:

- (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation {

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

SEL selector = NSSelectorFromString(@"setOrientation:");

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

[invocation setSelector:selector];

[invocation setTarget:[UIDevice currentDevice]];

int val = orientation;

[invocation setArgument:&val atIndex:2];

[invocation invoke];

}

}

- (void)setInterfaceOrientation:(UIDeviceOrientation)orientation {

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:orientation] forKey:@"orientation"];

}

}

你可能感兴趣的:(iOS横竖屏小记)