我终于想通了。我在原始帖子中没有提到的关键缺失组件是管理景观的视图控制器实际上是作为模态视图实现的。 (有关如何执行此操作的代码,请参阅 View Controller 用户指南)在概念上,我有一个 Portrait 视图控制器。 (这是主控制器)在纵向视图控制器的 viewDidLoad 中,我申请了一个通知程序,该通知程序由方向变化触发,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
// SECTION to setup automatic alternate landscape view on rotation
// Uses a delegate to bring the landscape view controller up as a modal view controller
isShowingLandscapeView = NO;
// Create Landscape Controller programmatically
self.landscapeViewController = [[LandscapeViewController alloc] initWithNibName:@"LandscapeViewController" bundle:[NSBundle mainBundle]];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
// END SECTION landscape modal view controller
然后,当方向改变时,这个方法被调用:
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
// Load Landscape view
landscapeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.landscapeViewController animated:YES];
isShowingLandscapeView = YES;
}
同时我正在从景观视图控制器的 viewWillAppear 方法中移除状态栏:
- (void)viewWillAppear:(BOOL)animated
{
// remove status bar from top of screen
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:animated];
self.myWebView.delegate = self; // setup the delegate as the web view is shown
}
这就是引入问题的地方。纵向视图控制器捕获屏幕尺寸,然后作为模态视图转换为横向。然后,viewWillAppear 在 Landscape 视图控制器中移除状态栏。
所以,解决办法是移动
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:animated];
在转换到横向模式视图之前,在纵向视图控制器中声明orientationChanged 方法。
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
// remove status bar from top of screen
// NOTE: this must be declared BEFORE presenting the Modal View!!!! If it's not, the landscape view will
// contain an ugly white bar in place of the missing status bar at the top of the view.
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// Load Landscape view
landscapeViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:self.landscapeViewController animated:YES];
isShowingLandscapeView = YES;
}
请注意,正如上面提到的 tc,如果您希望在返回纵向时显示状态栏,那么您需要
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:animated];
在景观视图控制器的 viewWillDisappear 方法中。