美文网首页iOS归纳
iOS 强制旋转app方向(含状态栏)

iOS 强制旋转app方向(含状态栏)

作者: 任梦RM | 来源:发表于2018-04-11 18:16 被阅读240次
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor whiteColor];
        [self setInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    //状态栏样式
    - (UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleDefault;
    }
    //状态栏显示/隐藏
    -(BOOL)prefersStatusBarHidden{
        return NO;
    }
    
    //强制转屏(这个方法最好放在BaseVController中)
    - (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation{
        if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
            SEL selector  = NSSelectorFromString(@"setOrientation:");
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
            [invocation setSelector:selector];
            [invocation setTarget:[UIDevice currentDevice]];
            // 从2开始是因为前两个参数已经被selector和target占用
            [invocation setArgument:&orientation atIndex:2];
            [invocation invoke];
        }
    }
    //必须返回YES
    - (BOOL)shouldAutorotate{
        return YES;
    }
    //旋转方向
    - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    
    //- (BOOL)shouldAutorotate{
    //    return NO;
    //}
    //- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    //    return UIInterfaceOrientationMaskLandscapeRight;
    //}
    //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    //    return UIInterfaceOrientationLandscapeRight;
    //}
    

    相关文章

      网友评论

        本文标题:iOS 强制旋转app方向(含状态栏)

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