前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS将单个控制器设为横屏、页面横屏

iOS将单个控制器设为横屏、页面横屏

作者头像
BY
发布2018-05-11 14:50:31
2.5K0
发布2018-05-11 14:50:31
举报
文章被收录于专栏:BY的专栏BY的专栏
最近项目中拍照页面需要设置为横屏,需求如下

进入拍摄页面后将页面强制设为横屏,拍照结束后回复竖屏。 简述为:A->B(横屏)

屏幕快照 2016-07-29 下午5.50.02.png

1. 首先在AppDelegate中添加一个公开属性restrictRotation并添加一个方法、该方法是是否允许屏幕转向
代码语言:javascript
复制
/** 允许转向 */
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.restrictRotation == YES)
        return UIInterfaceOrientationMaskLandscapeRight;
//        return UIInterfaceOrientationMaskLandscape;
    else
        
        return UIInterfaceOrientationMaskPortrait;
}
2. 在需要设置横屏的页面中添加下列方法
代码语言:javascript
复制
/**
 *  设置屏幕旋转
 *
 *  @param restriction yes or no
 */
- (void)restrictRotation:(BOOL) restriction {
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
    
}

在进入页面时允许屏幕旋转,并设置旋转的方向,代码如下

代码语言:javascript
复制
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
 
    [self restrictRotation:YES];
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    
}
// 离开时禁止旋转并将屏幕方向设为竖屏
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self restrictRotation:NO];
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

}

实现了将单个控制器设为横屏的功能。 但是,新的问题出现了:当B控制器返回A时,A控制器页变也为横屏(需要将手机转向才能恢复) 解决办法很简单: 在A控制器的-(void)viewWillAppear:(BOOL)animated方法中添加,再次设为竖屏即可

代码语言:javascript
复制
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.08.09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 最近项目中拍照页面需要设置为横屏,需求如下
    • 1. 首先在AppDelegate中添加一个公开属性restrictRotation并添加一个方法、该方法是是否允许屏幕转向
      • 2. 在需要设置横屏的页面中添加下列方法
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档