iOS 横竖屏控制
来源:互联网 发布:中国大数据时代好处 编辑:程序博客网 时间:2024/05/22 06:08
iOS 横竖屏控制
来自:增删查改地址:http://simayang.com/archives/405.html
公司App里面有个需求,即所有界面都是竖屏,且不允许横屏切换,唯独有一个图表界面允许横屏。那么,根据此需求处理如下:
首先,确保App本身应该允许转屏切换:
IOS屏幕旋转控制与旋转事件处理 设定App本身允许转屏
再次,我的App里面都是走UINavigationController进行界面push切换的,所以首先创建一个UINavigationController的子类,并设定允许转屏:
@implementation AppExtendNavigationController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark 转屏方法重写-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return [self.viewControllers.lastObject supportedInterfaceOrientations];}-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];}-(BOOL)shouldAutorotate{ return self.visibleViewController.shouldAutorotate;}
最后,在你不想转屏切换的ViewController上重写以下方法:
#pragma mark 转屏方法 不允许转屏-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait ;}- (BOOL)shouldAutorotate{ return NO;}-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationPortrait;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ return NO;}
在你想转屏切换的ViewController上可以照这样重写(允许左右横屏以及竖屏):
- (BOOL)shouldAutorotate { return YES;}-(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAll;}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return UIInterfaceOrientationPortrait;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ return YES;}
另外,在ViewController中对于转屏事件可以参见下面的方法进行捕获:
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{ [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { //计算旋转之后的宽度并赋值 CGSize screen = [UIScreen mainScreen].bounds.size; //界面处理逻辑 self.lineChartView.frame = CGRectMake(0, 30, screen.width, 200.0); //动画播放完成之后 if(screen.width > screen.height){ NSLog(@"横屏"); }else{ NSLog(@"竖屏"); } } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { NSLog(@"动画播放完之后处理"); }];}
区分当前屏幕是否为横竖屏的状态,其实通过判断当前屏幕的宽高来决定是不是横屏或者竖屏:
竖屏时:宽<高
横屏时:宽>高
以上在IOS8、9中测试通过
0 0
- ios 横竖屏控制
- iOS 横竖屏控制
- iOS 横竖屏控制
- ios 控制页面的横竖屏
- iOS开发中使用代码控制横竖屏的切换
- iOS强制转换横竖屏和键盘方向控制
- iOS8 横竖屏控制
- UI 横竖屏控制
- ios横竖屏解决方案
- ios横竖屏解决方案
- ios横竖屏问题
- ios横竖屏切换
- ios横竖屏解决方案
- iOS横竖屏解决方案
- iOS横竖屏解决方案
- iOS 横竖屏
- IOS横竖屏切换
- iOS横竖屏解决方案
- How to stop #including redundant headers?
- 进程管理
- swift.h和briging文件冲突,重复包含
- SDUT 2155 Emergency(Floyd)
- 前端框架OnsenUI学习之ng-repeat和ons-lazy-repeat介绍
- iOS 横竖屏控制
- 提高效率的Android Studio插件
- UIAutomatorview 简单封装 与 Appium Boostrap 结合
- hadoop、storm和spark的区别、比较
- spring ioc
- JAVA中的深度克隆(deep clone) 和 影子克隆(shallow clone)浅析
- Android studio 配置Git (第一次提交代码)
- Mysql非安装版的安装使用相关问题
- marathon参考(9):健康检查