有一个视图控制器(让我们将其命名为parent),它用presentModalViewController:animated:
表示其他视图控制器(叫它modal),然后旋转设备。两个控制器都支持所有方向。发生了什么:
- 旋转后,模态控制器立即接收
didRotateFromInterfaceOrientation:
- 就在解除模态控制器之前,父控制器也接收到
didRotateFromInterfaceOrientation:
。但是,只有当基本SDK设置为4.3(或更低)时,才会发生这种情况。如果将基本SDK设置为5,则父控制器不会收到此消息。在4.3中,我根据didRotateFromInterfaceOrientation:
中的新方向更新了父界面,在隐藏模式控制器后显示正确,但在iOS 5中,它没有被调用,在隐藏了模式控制器后父界面变得一团糟
所以,若设备在模态控制器可见的情况下旋转,我应该怎么做才能正确地将父控制器的界面更新到新的方向?
我遇到过类似的问题。我的解决方案是将方向处理代码移动到一个单独的方法中,并从旋转方法和viewWillAppear:
调用
//.h
@interface SomeViewController
- (void)layoutForInterfaceOrientation:(UIInterfaceOrientation)orientation;
@end
//.m
@implementation SomeViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];
[self layoutForInterfaceOrientation:currentOrientation];
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toOrientation duration:(NSTimeInterval)duration {
[self layoutForInterfaceOrientation:toOrientation];
}
- (void)layoutForInterfaceOrientation:(UIInterfaceOrientation)orientation {
// layout views
}
@end
这可能会解决问题。它对我有效:
modal.modalPresentationStyle = UIModalPresentationCurrentContext;