iOS强制屏幕旋转

最近项目需要展现一些图表,需要在用到的页面将页面设置为横屏,查阅整理出三个方案记录下来。其中方案一和方案二整个页面旋转,方案三只是view旋转,导航栏不会跟随旋转。

方案一:

  • AppDelegate.h中设置
/***  是否允许横屏的标记 */
@property (nonatomic, assign)BOOL allowRotation;
  • AppDelegate.m中设置
#pragma mark - 页面方向支持
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (void)setAllowRotation:(BOOL)allowRotation {
    
    _allowRotation = allowRotation;
    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;
        if (allowRotation) {
            val = UIInterfaceOrientationLandscapeRight;//横屏
        }else {
            val = UIDeviceOrientationPortrait;//竖屏
        }
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}
  • 在需要横屏的页面调用
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
  • 离开页面调用
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];  appDelegate.allowRotation = NO;

方案二:

  • 在横屏页
+ (void)rotationScreen {
    
    CGRect frame = [UIScreen mainScreen].bounds;
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.window.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
    app.window.bounds = CGRectMake(0, 0, frame.size.height, frame.size.width);
}
  • 离开页面
+ (void)recoverScreen {
    
    CGRect frame = [UIScreen mainScreen].bounds;
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.window.transform = CGAffineTransformIdentity;
    app.window.bounds = CGRectMake(0, 0, frame.size.width, frame.size.height);
}

方案三:

  • 在旋转页面
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:YES];
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    //在这里设置view.transform需要匹配的旋转角度的大小就可以了。
    self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
    [UIView commitAnimations];
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,241评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,314评论 25 708
  • 我叫小七,一个平凡的不能再平凡的女孩子,我喜欢拜仁慕尼黑,真的喜欢了太久了。显然拜仁已经成为了我的一部分,我...
    乔七儿阅读 339评论 0 1
  • 儿子小时候简直就是一个“小跟屁虫”,我走哪儿,他就跟哪儿。现在他都快六岁了,淘气“鬼”得不得了,倒成了他走哪儿我...
    简非花儿开阅读 322评论 0 0
  • 写了这么多默默无闻的老人,今天应该到我的老爹奶奶了,他们一直在记忆的最深处,微笑的看着我,奶奶依然清秀,老爹依然威...
    静晚阅读 236评论 0 3