一、给UItabBarController、UINavigationController,并添加如下代码
- (BOOL)shouldAutorotate{
return YES;
}
二、在appdelegate中添加一个属性_allowRotate
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
// NSLog(@"方向 ============= %ld", _allowRotate);
if ([_allowRotate isEqualToString:@"1"]) {
return UIInterfaceOrientationMaskAll;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
}
// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
if ([_allowRotate isEqualToString:@"1"]) {
return YES;
}
return NO;
}
三、最后在需要横屏的页面中添加如下代码
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
App_Delegate.allowRotate = @"1";
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
App_Delegate.allowRotate = @"0";
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 = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
四、在Deployment Info中设置如下