gpt4 book ai didi

ios - UIWindow 方向问题(两个 UIWindows 和横向模式)

转载 作者:可可西里 更新时间:2023-11-01 04:01:34 28 4
gpt4 key购买 nike

我知道这看起来是个大问题,但事实并非如此,所以请不要害怕阅读它。主要是我在解释这些东西是如何工作的。

我有两个 UIWindow在我的应用程序中。第一个是应用程序默认创建的主窗口。第二个叫modalWindow并且也在应用程序委托(delegate)中创建。

- (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;
}

我的大部分应用程序都是纵向的,但有一个 View Controller ,用户可以在其中切换到横向。我正在通过以下方式收听景观变化:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

这在 orientationChanged 中工作正常我介绍横向 View Controller 的方法。此时一切都很好。如果我旋转回纵向,我会得到 orientationChanged再次触发,此时我关闭了横向 View Controller 。

现在让我们解释一下第二个窗口是如何发挥作用的。在横向模式下,有一个 Action (只需按下按钮)在第二个窗口(modalWindow)上呈现一个新的 View Controller 。好的,让我们解释一下。

应用委托(delegate)有两个方法,如下所示:

- (void)presentActivityOnModalWindow:(UIViewController *)viewController
{
self.modalWindow.rootViewController = viewController;
self.modalWindow.hidden = NO;
}

- (void)modalTransitionFinished:(NSNotification *)notif
{
self.modalWindow.hidden = YES;
self.modalWindow.rootViewController = nil;
}

我调用 presentActivityOnModalWindow通过 [[[UIApplication sharedApplication] delegate] performSelector....] .我知道这不是最佳做法,但效果很好。至于解雇我用NSNotificationCenter发布关于解雇的通知。在 modalWindow 上呈现的 View Controller 仅支持纵向模式。它正在返回 YESshouldAutorotateUIInterfaceOrientationMaskPortraitsupportedInterfaceOrientations .

好的,解释了很多,但现在我们要解决问题了。当我旋转到横向,弹出 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当我们回到肖像模式时,不会回到正常框架。 如何解决这个问题?如果我需要提供更多详细信息,请随时询问。

最佳答案

系统只会处理您的keyWindow 的旋转。如果您有其他窗口,则必须自己处理旋转。

我也认为模态 Controller 是可行的方法。但是,如果您真的想处理旋转,请查看其他“自定义窗口”库如何处理旋转。警报 View 就是一个很好的例子:

https://github.com/search?o=desc&q=uialertview&s=stars&type=Repositories&utf8=

关于ios - UIWindow 方向问题(两个 UIWindows 和横向模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28625049/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com