UI篇-关于单个页面屏幕旋转要注意的问题

前言

有时候,我们会需要在整个项目中,使某一个ViewController支持屏幕旋转,而其他的ViewController并不能自动旋转。这是一个很常见的需求,下面就屏幕旋转相关问题做个小结。


强制页面旋转(假的屏幕旋转)

最多见的是,视屏播放中的横屏模式,点击全屏按钮,播放页面横屏最大化。使用
CGAffineTransformMakeRotation旋转操作配合动画即可。

-(void)fullScreenClick: (UIButton *)btn {
    btn.selected = !btn.selected;   
    if (btn.selected) {
    self.mySuperView = self.superview;
    [UIView animateWithDuration:0.3 animations:^{
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.transform = CGAffineTransformMakeRotation(M_PI / 2);
    } completion:nil];
    self.frame = self.bigFrame;

}else {
    [self removeFromSuperview];
    [self.mySuperView addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.transform = CGAffineTransformMakeRotation(M_PI * 2);
    } completion:nil];
    self.frame = self.smallFrame;
  }  
}

强制带有导航条的整个页面旋转

- (void)changeScreent :(BOOL)toRight
{
    CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    [UIView animateWithDuration:duration animations:^{
        // 修改状态栏的方向及view的方向进而强制旋转屏幕
        [[UIApplication sharedApplication] setStatusBarHidden:toRight];
        self.navigationController.view.transform = toRight?CGAffineTransformMakeRotation(M_PI_2): CGAffineTransformIdentity;
        if (toRight) {
            self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
        }else{
            self.navigationController.view.bounds = CGRectMake(self.navigationController.view.bounds.origin.x, self.navigationController.view.bounds.origin.y, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
        }   
    }];
    
}

注意状态栏无法强制旋转,在手机方向不变的情况下,所以,最好把状态栏隐藏掉,回复的时候再显示出来。不可使用self.view.frame.size.width self.view.frame.size.height,这样会出现第一次旋转出现上下部分白边的Bug,需要使用 [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height。

自动屏幕旋转

系统支持横屏的顺序

系统支持横屏顺序为以下几种,前面的会使后面的方法失效,优先级依次降低。

  • 默认读取plist里面设置的方向(优先级最高)等同于Xcode Geneal设置里面勾选
Paste_Image.png
  • application window设置的级别次之

    application支持所有
    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;//支持所有方向
    }
    
  • UINavigationcontroller里设置

     - (BOOL)shouldAutorotate//是否支持旋转屏幕{
      return YES;
    }
    - (NSUInteger)supportedInterfaceOrientations//支持哪些方向{
      return UIInterfaceOrientationMaskPortrait;
    }
    
  • 级别最低的是viewcontroller里的设置

如何实现某一个页面屏幕旋转,而其他页面不旋转

首先, - (BOOL)shouldAutorotate 必须在 self.window.rootViewController 中才能有效果,而且每当手机发生旋转时,就会掉用 rootViewController 的 - (BOOL)shouldAutorotate 方法。

首先我们要保证工程设置为未勾选的状态才行。

Paste_Image.png

一般我们的rootViewController 都是UINavigationcontroller ,所有我们在UINavigationcontroller中设置如下方法
#获取栈最顶端的controller对旋转的支持状态即可,
#然后在每一个VC中都要设置 - (BOOL)shouldAutorotate 来确定当前的VC是否支持横竖屏
# 如果支持,还需要设置 - (NSUInteger)supportedInterfaceOrientations//支持哪些方向

- (BOOL)shouldAutorotate
{
   return  self.topViewController.shouldAutorotate;
}

如果rootViewController 都是 tarBarController ,所有我们在tarBarController中设置如下方法

- (BOOL)shouldAutorotate{

   return  self.selectedViewController.shouldAutorotate;

}

这样的情况下,每个VC都是默认支持旋转的,那么我们需要在每个VC中都设置- (BOOL)shouldAutorotate吗?答案是否定的。我们可以创建一个controller的基类BaseViewController每个controller都继承BaseViewController ,在BaseViewController中重写- (BOOL)shouldAutorotate 方法中 return NO; 默认关闭,,在需要开启的子类中再次重写- (BOOL)shouldAutorotate 方法,在方法中return YES即可。

下面是一个支持旋转屏幕VC的代码
- (BOOL)shouldAutorotate
 {
      return  YES;
 }
 # 点击全屏
 - (IBAction)large:(id)sender
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
 # 全屏返回
- (IBAction)largeBack:(id)sender
{
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
# 在这个方法中设置屏幕旋转时的 页面设置。
- (void)willRotateToInterfaceOrientation:    (UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
    self.navigationController.navigationBarHidden = NO;
    self.toolBar.hidden = NO;
    self.largeBackButton.hidden = YES;
    self.bottomView.hidden = NO;
    self.largeTitleLabel.hidden = YES;
    self.localRecrodContraint.constant = 10;
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
       toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.navigationController.navigationBarHidden = YES;
        self.localRecrodContraint.constant = 50;
        self.toolBar.hidden = YES;
        self.largeTitleLabel.hidden = NO;
        self.largeBackButton.hidden = NO;
        self.bottomView.hidden = YES;
    }
}

******************更新****************

上面的方法完美解决了我一个工程的单页面屏幕旋转问题,可是在另一个工程中,上面的方法确出现了一个Bug,真的很蛋疼。

按照上面的方法我确实达到了,单页面旋转,其他页面不旋转的效果,但是有个问题: 在页面不旋转的情况下,状态栏确会随着手机的旋转而旋转,着实蛋疼。目前不清楚为什么一样的设置,在两个项目中效果不一样。

解决方法:

 # AppDelegate 中
@property (strong, nonatomic) MyNavigationController* nav;  
 //手机方向发生变化时就会掉用
   手机方向不发生变化时就不会掉用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return self.nav.topViewController.supportedInterfaceOrientations;//支持所有方向
}
 # MyNavigationController中
- (BOOL)shouldAutorotate
{
    return  self.topViewController.shouldAutorotate;   
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return self.topViewController.supportedInterfaceOrientations;
}
 # BaseViewController中
- (BOOL)shouldAutorotate
{
    return NO;
}
 //不设置的话,虽然页面是没有旋转,但是状态栏会随着手机旋转而旋转。
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

 #需要旋转的VC中
- (BOOL)shouldAutorotate
{
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

可见,状态栏的方向跟页面支持的方向是一样的。所以页面支持的方向是一定要设定的,不设定的话就是默认的:左中右。

屏幕旋转中的其它问题
  • 如何应用程序刚启动时判断设备方向呢?之前说的那些都是都是在rootViewController之后去判断的,但是,在程序刚刚启动时做这些判断都是无效的。下面是网上的一个方法(未验证),在didFinishLaunchingWithOptions函数中:

    //注册通知
    UIDevice *device = [UIDevice currentDevice];
    [device beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenterdefaultCenter] addObserver: self
             selector: @selector(deviceOrientationDidChangeAction:)
                                               name: UIDeviceOrientationDidChangeNotification
                                             object: nil];  
    
    [device endGeneratingDeviceOrientationNotifications];
     //转屏处理函数:
    
    - (void) deviceOrientationDidChangeAction:(NSNotification *)note
    
    {
    
       NSInteger currentOrientation = [[note object] orientation];
    
       switch (currentOrientation)  {
              case0: {   //未知方向
                 break;
          }
          case1: {   //home键向下
                break;
          }
          case2: {   //home键向上
                break;
          }
          case3: {  //home键向左
                break;
          }
          case4: {  //home键向右
                 break;
          }
          default:
                break;
      }
    }
    
    还要在恰当的时候移除通知  不然会被反复调用:
    
    [[NSNotificationCenterdefaultCenter]   removeObserver:self  name:UIDeviceOrientationDidChangeNotification   object:nil];
    
  • 屏幕旋转时,状态栏会默认隐藏的,如何显示出来

    //iOS8 横屏的时候系统默认隐藏了
    [UIApplication sharedApplication].statusBarHidden = YES;
    [UIApplication sharedApplication].statusBarHidden = NO;
    
     # 请注意,上面的俩条一条都不可以少,而且也不可以颠倒顺序。我看着也醉了。但是却是有作用。
    

小结

关于屏幕旋转的问题,目前先写这些,后续如果有新的东西收获,会更新上去的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容