我有一个UIViewController,其中包含许多子视图,如果iPad处于横向或纵向模式,我需要以不同的顺序布置它们。我想获取UIViewController视图在旋转后将要转换成的大小,以便我可以计算我的子视图的新框架集。然而,在旋转发生之前,我没有找到任何获取这个新大小的方法。当旋转发生时,已经太晚了,因为我无法将子视图过渡到它们的新位置。
有人知道解决这个问题的方法吗?
我有一个UIViewController,其中包含许多子视图,如果iPad处于横向或纵向模式,我需要以不同的顺序布置它们。我想获取UIViewController视图在旋转后将要转换成的大小,以便我可以计算我的子视图的新框架集。然而,在旋转发生之前,我没有找到任何获取这个新大小的方法。当旋转发生时,已经太晚了,因为我无法将子视图过渡到它们的新位置。
有人知道解决这个问题的方法吗?
willAnimateRotationToInterfaceOrientation:duration:
吗?正如文档所述:在头文件中定义你的landscape
和portrait
视图,在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];
来动画到新视图。
willAnimateRotationToInterfaceOrientation:duration
中设置视图的宽度,那么视图最终会在侧边留下状态栏大小的间隙。 - Aaron