iOS的push方法推出控制器界面中单个屏幕的旋转问题

前言

相信很多人都遇到屏幕旋转的的问题,尤其push方法推出控制器的view的时候,很遇到很多的问题,我之前遇到问题就是很坑的问题,连续反复push方法、pop方法推出和退出需要横屏控制器的显示的时候,就会返回的时候出现横屏显示着怎么也看不出来是哪里的问题。


问题

开始屏幕是这样的
这里写图片描述
点击进入之后是是这样的
这里写图片描述
然后反复点击几次退出进入之后返回屏幕还是一直横屏,并没有竖起屏幕,一直都找不到什么原因
这里写图片描述


解决办法以及细节代码设置

push进去的控制器肯定要支持横屏,所以必须要有这句话

设置可以旋转
- (BOOL)shouldAutorotate{
    return YES;
}
支持的旋转方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}
默认的方向
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

    return UIInterfaceOrientationLandscapeLeft;

}

同理的实现push方法的控制器的肯定不支持横屏

-(BOOL)shouldAutorotate
{  //允许旋转
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{   
    return UIInterfaceOrientationMaskPortrait;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{   // 返回nav栈中最后一个对象,坚持旋转的方向
    return UIInterfaceOrientationPortrait;
}

既然要旋转屏幕什么时候旋转是个问题,我们必须知道屏幕方向的改变吧,所以肯定添加观察者来监控屏幕的方向的改变

开启和监听 设备旋转的通知(不开启的话,设备方向一直是UIInterfaceOrientationUnknownif (![UIDevice currentDevice].generatesDeviceOrientationNotifications) {
        [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

当然肯定我们知道屏幕的方向,就可以设置屏幕的旋转了,我们也可以通过手动设置来改变屏幕的方向, (setOrientation:已经被设置为私有方法,上架会被拒,但是kvc的强大可以解决)

- (void)buttonFullOnClick:(BOOL)isFull{

    if (isFull) {
         [self enterToLandscape];
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
    }else{
        [self backToPortrait];
        [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
    }
}

最后关键的地方来了,也是我自己犯错的地方,这个是我写返回按钮点击事件写的方法,这个方法导致了我返回的时候出现横屏的问题,所以我重点说明一下

- (void)backBtnOnClick{
    WeakSelf
    [LYTool showAlterViewWithTitle:nil message:@"确认退出观看?" completeBlock:^(NSInteger index) {
        if (index == 1) {
            if (_isHalfScreen == NO) {
                [weakSelf buttonFullOnClick:NO];
            }
            weakSelf.navigationController.navigationBarHidden = YES;
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}

2、点击切换为小屏时
- (void)smallScreen{
    _isHalfScreen  = YES;
    [UIView animateWithDuration:0.01 animations:^{
        _player.view.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    } completion:^(BOOL finished) {
//        [self.navigationController popViewControllerAnimated:YES];
    }];
//    [self setNeedsStatusBarAppearanceUpdate];
}

设备方向改变的处理

- (void)handleDeviceOrientationChange:(NSNotification *)notification{

    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    switch (deviceOrientation) {
        case UIDeviceOrientationLandscapeLeft:
            NSLog(@"屏幕向左横置");
            [self fullScreen];
            break;
        case UIDeviceOrientationLandscapeRight:
            NSLog(@"屏幕向右橫置");
            [self fullScreen];
            break;
        case UIDeviceOrientationPortrait:
            NSLog(@"屏幕直立");
            [self smallScreen];
            break;
        case UIDeviceOrientationPortraitUpsideDown:
            NSLog(@"屏幕直立,上下顛倒");
//            [self smallScreen];
            break;
        default:
            NSLog(@"无法辨识");
            break;
    }

}

上面的 2、点击切换为小屏时 其实就是屏幕的旋转成横屏的时候会调用的方法 ,也就是屏幕方向改变的时候,会调用的方法,从而调用方法2。重点就是这一句的位置。

[self.navigationController popViewControllerAnimated:YES];

我开始的时候是放在上面的位置,连续三次反复之后,就出现屏幕一直横屏的情况,是不是很坑,其实想一想也是这么回事,以为屏幕的旋转是有动画,是要时间的,但是这个方法调用却是很快,就会存在还没有旋转回来就出现已经返回上一个界面,然后界面就被释放了,理所当然就不会调用那个方法就不会有用,要在动画旋转结束的completion:(^)回调中执行上面的方法,这样就完美解决刚才的问题,之前把自己坑坏了呀

总结

就是自己给自己挖坑,然后自己慢慢花时间填坑,MMP一句终结本篇。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值