我知道这看起来像一个很大的问题,但其实并不是,所以不要害怕阅读。主要是我在解释东西是如何工作的。
我的应用程序中有两个UIWindow
。第一个是默认情况下由应用程序创建的主窗口。第二个称为modalWindow
,也是在应用程序委托中创建的。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.modalWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.modalWindow.windowLevel = UIWindowLevelStatusBar;
//Other stuff I need
//....
return YES;
}
我的应用程序大部分都是竖屏的,但有一个视图控制器可以让用户切换到横屏。我通过以下方式监听横屏变化:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
这段代码运行正常,在
orientationChanged
方法中,我呈现了横向视图控制器。此时一切都很好。如果我旋转回纵向模式,orientationChanged
再次触发,此时我会关闭横向视图控制器。现在让我们解释第二个窗口是如何起作用的。当处于横向模式时,有一个操作(只需按下按钮),它会在第二个窗口(
modalWindow
)上呈现一个新的视图控制器。好的,让我们来解释一下。应用程序委托有两种方法,如下所示:
- (void)presentActivityOnModalWindow:(UIViewController *)viewController
{
self.modalWindow.rootViewController = viewController;
self.modalWindow.hidden = NO;
}
- (void)modalTransitionFinished:(NSNotification *)notif
{
self.modalWindow.hidden = YES;
self.modalWindow.rootViewController = nil;
}
我通过
[[[UIApplication sharedApplication] delegate] performSelector....]
调用presentActivityOnModalWindow
。我知道这不是最佳实践,但它可以正常工作。至于关闭,我使用NSNotificationCenter
发布有关关闭的通知。在modalWindow
上呈现的视图控制器仅支持纵向模式。它在shouldAutorotate
中返回YES
,并在supportedInterfaceOrientations
中返回UIInterfaceOrientationMaskPortrait
。好的,解释了很多,现在我们来到问题。当我旋转到横向、弹出modalWindow、关闭modalWindow并旋转回纵向时,我的主窗口仍处于横向模式,一切看起来都很糟糕。
我尝试给
window
和modalWindow
添加观察者,并记录frame
和bounds
的变化,以下是结果:Did finish launching:
window frame {{0, 0}, {320, 568}}
window bounds {{0, 0}, {320, 568}}
modalWindow frame {{0, 0}, {320, 568}}
modalWindow bounds {{0, 0}, {320, 568}}
Rotate to landscape:
window frame {{0, 0}, {568, 320}}
Open activity in landscape:
modalWindow frame {{0, 0}, {320, 568}}
modalWindow frame {{0, 0}, {320, 568}}
Dismiss activity:
none
Rotate back to portrait:
none
看起来当我们回到纵向模式时,window
不能返回正常的框架。 如何解决这个问题? 如果需要提供更多细节,请随时询问。