如题,分为场景A和场景B,
场景A下,ViewController能旋转;
场景B下,ViewController不能旋转。
在 ViewController 中添加 shouldAutorotate
方法,里面的逻辑大致如下:
@implementation ViewController
- (BOOL)shouldAutorotate {
int32_t i = 1;
if (i == 1) {
return NO;
}
return YES;
}
@end
如果你的 ViewController 是管理在 UINavigationController 中,则需要去修改 UINavigationController 对应的行为,比如通过 Category 的方式实现思路大致如下:
#import "UINavigationController.h"
@implementation UINavigationController (Overrides)
- (BOOL)shouldAutorotate {
id currentViewController = self.topViewController;
if ([currentViewController isKindOfClass:[<#your-vc#> class]]) {
int32_t i = 1;
if (i == 1) {
return NO;
}
return YES;
}
return YES;
}
@end
经过多番尝试,发现:想要旋转设备的时候,访问shouldAutorotate该方法,info.plist中不能把支持的方向全勾上。其次旋转的时候,访问的是跟ViewController的shouldAutorotate。
感谢,大家的帮助。
使用shouldAutorotate,具体方法见SamirChen
当用户旋转的时候系统会问Info.plist和顶级的ViewController,如果shouldAutorotate返回NO/Info.plist不允许旋转,则不能旋转