【问题标题】:Getting the size of a UIViewController's view after rotation旋转后获取 UIViewController 视图的大小
【发布时间】:2010-12-13 18:58:39
【问题描述】:

我有一个 UIViewController 和一堆子视图,如果 iPad 处于纵向或横向模式,我需要以不同的顺序布置这些子视图。我希望能够获取 UIViewController 的视图在旋转后要转换的大小,以便我可以为我的子视图计算新的帧集。但是,在旋转发生之前,我还没有找到任何方法来获得这个新大小,当旋转发生时已经太晚了,因为我无法动画子视图到它们的新位置的过渡。

有人知道解决这个问题的方法吗?

【问题讨论】:

    标签: iphone objective-c ios ios4


    【解决方案1】:

    你试过willAnimateRotationToInterfaceOrientation:duration:。正如文档所述:

    调用此方法时,interfaceOrientation 属性已设置为新方向。因此,您可以在此方法中执行视图所需的任何其他布局。

    我相信视图控制器的视图在这个方法中也应该有它的新维度。

    【讨论】:

    • 我目前正在尝试解决同样的问题,但我发现视图还没有新的维度。如果我在willAnimateRotationToInterfaceOrientation:duration 中设置视图的宽度,那么视图最终会在侧面出现一个状态栏大小的间隙。
    【解决方案2】:

    在标题中定义 landscapeportrait 视图,将它们附加到界面生成器中,然后尝试以下代码:

    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
         if(((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
          (interfaceOrientation == UIInterfaceOrientationLandscapeRight))){
    
            self.view = landscape;
    
     }else if(((interfaceOrientation == UIInterfaceOrientationPortrait) || 
         (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown))){
    
            self.view = portrait;
    
        }
        return YES;
    }
    

    这比为视图中的每个对象计算新帧要容易得多。

    要制作动画,您可以输入 [UIView beginAnimations:@"animation" context:nil];[UIView commitAnimations]; 以制作新视图的动画。

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多