美文网首页好东西小知识点
iOS 控制屏幕旋转的方法

iOS 控制屏幕旋转的方法

作者: 当阳桥 | 来源:发表于2017-02-09 15:00 被阅读17次

    1.在AppDelegate中添加代码

    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    @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 控制屏幕旋转的方法

        本文链接:https://www.haomeiwen.com/subject/qxliittx.html