【问题标题】:UIViewController interface orientation is wrongUIViewController 界面方向错误
【发布时间】:2014-08-11 12:28:04
【问题描述】:

UIViewController 旋转方法中,我在执行设备旋转时遇到问题

方法内部

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

我得到的值不正确

根据文档:

在用户界面开始之前发送到视图控制器 旋转。子类可以重写此方法以执行额外的 轮换前的动作。例如,您可以使用 此方法用于禁用视图交互、停止媒体播放或 暂时关闭昂贵的绘图或实时更新。你也可能 使用它将当前视图交换为反映新视图的视图 界面方向。 当这个方法被调用时, interfaceOrientation 属性仍然包含视图的原始 方向。你的这个方法的实现必须调用 super at 在执行过程中的某个时间点。这个方法被调用,不管 您的代码是执行一步还是两步旋转。

我正举起设备(在启动应用程序后设备物理上处于纵向模式) - 所有视图都正确对齐纵向模式

但是在方法中我得到了错误的值:

    -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
    // getting the wrong interface orientation here!!!!
   // just checking the current orientation for debug
        UIInterfaceOrientation currentOrientation = self.interfaceOrientation;
   // the first time the device is rotated getting this value for current orientation:
        // currentOrientation == UIInterfaceOrientationLandscapeRight
    }

比这种奇怪的错误方向更糟糕的情况是在“将旋转到界面方向”的相同方法中,当前界面方向和目标界面方向相同,即:

self.interfaceOrientation === toInterfaceOrientation

那么框架调用这个方法的意义何在???

这会导致我的所有 UIView 放置代码在设备初始旋转时都无法正确计算。

为什么这个标志设置不正确?!?!

附言

第二次旋转设备后 - 标志设置正确,视图“对齐”自身正确。

【问题讨论】:

    标签: ios objective-c uiviewcontroller uiinterfaceorientation


    【解决方案1】:

    你会得到正确的值,假设你在端口仪式中拿着设备,然后你把它变成横向模式,然后这个方法被调用

    //先调用

     -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
      {
          //check which orientation hear   
          if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
           {
                 NSLog(@"turning t landscape");
           }
          else
           {
                 NSLog(@"turning t portrite");
           }
      }
    

    调用hear toInterfaceOrientation 的方法包含(对于我们的示例)UIInterfaceOrientationLandscapeRightorUIInterfaceOrientationLandscapeLeft`,这表明设备将转向横向左或仪式

    在此之后,按顺序调用以下方法

     - (void)viewWillLayoutSubviews  
    
     - (void)willAnimateRotationToInterfaceOrientation:duration:
    
     - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
    

    被调用,在最后一个方法中听到 fromInterfaceOrientation 包含(对于我们的示例)旧值或先前的方向标志,即 UIInterfaceOrientationPortraitUpsideDownUIInterfaceOrientationPortrait

    你想知道当前方向然后你可以使用

     UIInterfaceOrientation  orientation = [[UIApplication sharedApplication] statusBarOrientation];
      if(UIInterfaceOrientationIsLandscape(orientation))
      {
        NSLog(@"landscape");
      }
      else
      {
        NSLog(@"portrite");
      }
    

    有关此的更多信息,您可以查看docs

    【讨论】:

    • 那根本不正确。当前界面方向不应在此阶段更改,并且在任何方向更改方法中绝对不应等于“toInterfaceOrientation”(使用大写字母 EVERRRRR)。这完全没有意义
    • 这是调用方向方法的方式,请参阅我链接的文档,它有很好的解释......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    相关资源
    最近更新 更多