iOS 控制屏幕旋转的方法

1.在AppDelegate中添加代码

@interface AppDelegate : UIResponder 
@property (assign, nonatomic) BOOL allowRotation;
@end
@implementation AppDelegate
// 屏幕每次将要旋转时候会调用这个方法
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (_allowRotation) {
        return UIInterfaceOrientationMaskAll;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

2.在控制器的viewWillAppear中添加

// 进入支持旋转的页面添加
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    AppDelegate *delegate =  (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotation = YES;   
}
// 离开支持旋转的页面添加
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    AppDelegate *delegate =  (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotation = NO;
}

参考
http://www.warting.com/program/201603/148923.html
http://www.jianshu.com/p/73be6d0e152f
http://www.tuicool.com/articles/nUJbuuM/

你可能感兴趣的:(iOS 控制屏幕旋转的方法)