1

我正在尝试创建两个 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;

}

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

"<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>>"

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

提前致谢!

4

5 回答 5

1

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

window2.windowLevel = UIWindowLevelNormal + 1;

PS:

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

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

于 2013-05-20T03:24:57.960 回答
0

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

于 2011-04-29T22:08:51.670 回答
0

只需使用UISplitViewController.

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

于 2011-04-29T00:02:20.863 回答
0

这可能是一个非常老的帖子,但我遇到了同样的问题。一些编码错误已经得到解答,但我们这里的主要问题是您如何实例化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
    }
}

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

于 2015-09-14T07:55:58.430 回答
-2

试试这个代码...

id delegate = [[UIApplication sharedApplication] delegate];
[[delegate FirstView] presentModalViewController:SecondView animated:YES];
于 2012-06-06T06:30:26.007 回答