iOS 横竖屏控制
2016-04-11 10:56
525 查看
在公司的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;
}
在你App项目中不想支持横屏的界面(不支持转屏切换),添加如下代码:
#pragma mark 转屏方法 不允许横屏,只支持竖屏显示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return NO;
}
在你App项目中想支持横屏显示(转屏切换),添加如下代码:
- (BOOL)shouldAutorotate {
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
如果你想捕获旋转屏幕的事件,你可以添加如下代码,在这几个方法里面做相应的处理,代码如下:
第一,我们要确保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;
}
在你App项目中不想支持横屏的界面(不支持转屏切换),添加如下代码:
#pragma mark 转屏方法 不允许横屏,只支持竖屏显示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return NO;
}
在你App项目中想支持横屏显示(转屏切换),添加如下代码:
- (BOOL)shouldAutorotate {
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
如果你想捕获旋转屏幕的事件,你可以添加如下代码,在这几个方法里面做相应的处理,代码如下:
</pre><pre name="code" class="objc">- (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(@"动画播放完之后逻辑处理"); }]; }
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 个人信息安全报告发布:有 APP 每分钟调用位置权限 1468 次
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- 下载量超过一亿的流行应用被发现含有恶意模块
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 苹果与Siri的七年之痒:“宫斗”戏码不断上演
- 央视新闻报道XcodeGhost事件
- 原生JS仿苹果任务栏菜单,放大效果的菜单
- C#生成Word文档代码示例
- 如何使用C#从word文档中提取图片
- APP添加CNZZ统计插件教程 Android版添加phonegap
- jQuery窗口、文档、网页各种高度的精确理解
- 比较全的一个C#操作word文档示例
- Android APP与媒体存储服务的交互
- java实现收藏名言语句台词的app