旋转后获取UIViewController的视图大小

3

我有一个UIViewController,其中包含许多子视图,如果iPad处于横向或纵向模式,我需要以不同的顺序布置它们。我想获取UIViewController视图在旋转后将要转换成的大小,以便我可以计算我的子视图的新框架集。然而,在旋转发生之前,我没有找到任何获取这个新大小的方法。当旋转发生时,已经太晚了,因为我无法将子视图过渡到它们的新位置。

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

2个回答

3
你尝试过willAnimateRotationToInterfaceOrientation:duration:吗?正如文档所述:
“在调用此方法时,interfaceOrientation属性已设置为新方向。因此,您可以在此方法中执行视图所需的任何其他布局。”
我认为视图控制器的视图在此方法中也应具有其新尺寸。

我目前正在尝试解决同样的问题,我发现视图还没有其新的尺寸。如果我在 willAnimateRotationToInterfaceOrientation:duration 中设置视图的宽度,那么视图最终会在侧边留下状态栏大小的间隙。 - Aaron

1

在头文件中定义你的landscapeportrait视图,在Interface Builder中附加它们,然后尝试这段代码:

-(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]; 来动画到新视图。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接