iOS的push方法推出控制器界面中单个屏幕的旋转问题
2018-01-08 19:20
1171 查看
前言
相信很多人都遇到屏幕旋转的的问题,尤其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; }
既然要旋转屏幕什么时候旋转是个问题,我们必须知道屏幕方向的改变吧,所以肯定添加观察者来监控屏幕的方向的改变
开启和监听 设备旋转的通知(不开启的话,设备方向一直是UIInterfaceOrientationUnknown) if (![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一句终结本篇。相关文章推荐
- iOS开发中带导航控制器的屏幕旋转问题
- iOS 6.0屏幕旋转的问题
- IOS 屏幕旋转相关问题
- iOS两个强制旋转屏幕的方法
- iOS 屏幕旋转问题
- 关于IOS屏幕的旋转问题
- ios 6.0 屏幕旋转问题
- ios 关于禁止屏幕旋转问题的解决
- iOS 开发,屏幕旋转、旋转屏幕collectionView cell 的自动布局问题
- IOS Orientation, 想怎么转就怎么转~~~ 此博文主要针对IOS应用, 是屏幕旋转相关问题的一个总结. 主要内容有: IOS5,6,7不同版的适配. 强制旋转和自动旋转.
- iOS从当前隐藏导航界面push到下一个显示导航界面出现闪一下的问题
- IOS成长之路-关于iOS6.0 屏幕旋转的问题
- ios(ipad,iphone)屏幕旋转检测通用方法
- iOS两个强制旋转屏幕的方法
- 关于iOS6.0 屏幕旋转的问题
- iOS两个强制旋转屏幕的方法
- iOS从当前隐藏导航界面push到下一个显示导航界面出现闪一下的问题
- iPhone开发笔记(21)iOS 6旋转问题解决方法汇总
- 单个控制器的屏幕旋转问题
- iPhone开发笔记(21)iOS 6旋转问题解决方法汇总