第二个 UIWindow 未显示 (iPad)

发布于 2024-11-04 15:52:00 字数 1439 浏览 2 评论 0 原文

我正在尝试创建两个 UIWindows,因为我希望在我的应用程序上同时在屏幕上显示两个 UINavigationController。我在应用程序委托中初始化了两个窗口,但只显示一个窗口的视图。有谁知道为什么会这样?

这是我使用的代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    UIViewController * controller1 = [[UIViewController alloc] init];
    [controller1.view setBackgroundColor:[UIColor grayColor]];
    UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:controller1];
    [window addSubview:nav1.view];
    [window makeKeyAndVisible];

    UIWindow * window2 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    UIViewController * controller2 = [[UIViewController alloc] init];
    [controller2.view setBackgroundColor:[UIColor yellowColor]];
    UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:controller2];
    [window2 addSubview:nav2.view];
    [window2 makeKeyAndVisible];


    NSLog(@"%@", [[UIApplication sharedApplication] windows]);



    return YES;

}

第一个窗口中的灰色可见,但第二个窗口中的黄色不可见。输出为:

">", “>”

这意味着第二个窗口已创建并添加到应用程序中,但只是不显示。有谁知道为什么会这样?

提前致谢!

I am attempting to create two UIWindows because I would like two UINavigationControllers on screen at the same time on my app. I initialize two windows in my app delegate but only one window's view is displayed. Does anyone know why this is so?

Here is the code I used:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    UIViewController * controller1 = [[UIViewController alloc] init];
    [controller1.view setBackgroundColor:[UIColor grayColor]];
    UINavigationController * nav1 = [[UINavigationController alloc] initWithRootViewController:controller1];
    [window addSubview:nav1.view];
    [window makeKeyAndVisible];

    UIWindow * window2 = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    UIViewController * controller2 = [[UIViewController alloc] init];
    [controller2.view setBackgroundColor:[UIColor yellowColor]];
    UINavigationController * nav2 = [[UINavigationController alloc] initWithRootViewController:controller2];
    [window2 addSubview:nav2.view];
    [window2 makeKeyAndVisible];


    NSLog(@"%@", [[UIApplication sharedApplication] windows]);



    return YES;

}

The gray from the first window is visible, but the yellow from the second is not. The output from this is:

"<UIWindow: 0x591e650; frame = (0 0; 768 1024); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x591e7a0>>",
"<UIWindow: 0x5923920; frame = (0 0; 100 100); layer = <CALayer: 0x59239a0>>"

which means the second window is created and added to the application, but just not displayed. Does anyone know why this is so?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城半夏 2024-11-11 15:52:01

我发现了如何显示第二个 UIWindow。您必须将 ClipsToBound 属性设置为 YES。否则,其中一个窗口的视图将完全覆盖另一个窗口的视图。毕竟这两个窗口已正确添加并且可见。

I've discovered how to get the second UIWindow to display. You must set the clipsToBound property to YES. Otherwise, the view from one of the windows will completely cover the other view. The two windows were properly added and visible after all.

穿透光 2024-11-11 15:52:01

这可能是一篇非常旧的帖子,但我遇到了同样的问题。一些编码错误已经得到解答,但我们这里遇到的主要问题是如何实例化 UIWindow

这是一个如何正确显示另一个 UIWindow 的 Swift 示例。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow? = UIWindow(frame: UIScreen.mainScreen().bounds)

    let newWindow = UIWindow(frame: UIScreen.mainScreen().bounds) 
    // save a reference to your Window so it won't be released by ARC

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        self.window!.rootViewController = SomeViewController()
        self.window!.makeKeyAndVisible()

        // in your example you have created the window inside this method,
        // which executes correctly and at the end of this method just releases the window,
        // because you never saved the reference to the window
        self.newWindow.rootViewController = SomeOtherViewController()
        self.newWindow.windowLevel = UIWindowLevelStatusBar + 1.0
        self.newWindow.hidden = false

        return true
    }
}

顺便提一句。您不必在 AppDelegate 中创建 UIWindow。这取决于您的代码行为。

This might be a really old post but I just run into the same problem. Some coding mistakes where already answered but the main issue we have here is how you instantiating the UIWindow.

Here is a Swift example how to display another UIWindow correctly.

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow? = UIWindow(frame: UIScreen.mainScreen().bounds)

    let newWindow = UIWindow(frame: UIScreen.mainScreen().bounds) 
    // save a reference to your Window so it won't be released by ARC

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        self.window!.rootViewController = SomeViewController()
        self.window!.makeKeyAndVisible()

        // in your example you have created the window inside this method,
        // which executes correctly and at the end of this method just releases the window,
        // because you never saved the reference to the window
        self.newWindow.rootViewController = SomeOtherViewController()
        self.newWindow.windowLevel = UIWindowLevelStatusBar + 1.0
        self.newWindow.hidden = false

        return true
    }
}

Btw. you don't have to create a UIWindow in AppDelegate. It depends on your code behavior.

嘿咻 2024-11-11 15:52:01

试试这个代码...

id delegate = [[UIApplication sharedApplication] delegate];
[[delegate FirstView] presentModalViewController:SecondView animated:YES];

try this code...

id delegate = [[UIApplication sharedApplication] delegate];
[[delegate FirstView] presentModalViewController:SecondView animated:YES];
黄昏下泛黄的笔记 2024-11-11 15:52:00

两个UIWindow的windowLevel属性是相等的,它们都是UIWindowLevelNormal
如果你想让第二个UIWindow显示第一个UIWindow的字体,你应该将第二个UIWindow的windowLevel值设置得更大。如:

window2.windowLevel = UIWindowLevelNormal + 1;

PS:

[window makeKeyAndVisible];  
...  
[window2 makeKeyAndVisible];

一次只有一个 keyWindow,按键窗口是指定接收键盘和其他非触摸相关事件的窗口。一次只有一个窗口可能是关键窗口。

The two UIWindow's windowLevel property is equal, they are all UIWindowLevelNormal.
If you want the second UIWindow display font of the first UIWindow, You should set the second UIWindow's windowLevel value bigger. Like:

window2.windowLevel = UIWindowLevelNormal + 1;

PS:

[window makeKeyAndVisible];  
...  
[window2 makeKeyAndVisible];

There is only one keyWindow at a time, The key window is the one that is designated to receive keyboard and other non-touch related events. Only one window at a time may be the key window.

请持续率性 2024-11-11 15:52:00

只需使用 UISplitViewController 即可。

或者如果您需要自定义,请尝试 MGSplitVIewController 。它可能有你需要的东西。

Just use a UISplitViewController.

Or try MGSplitVIewController if you need to customization. It might have what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文