【问题标题】:Second UIWindow Not Displaying (iPad)第二个 UIWindow 不显示(iPad)
【发布时间】:2011-04-28 23:58:50
【问题描述】:

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

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

提前致谢!

【问题讨论】:

    标签: cocoa-touch ios ipad uiwindow


    【解决方案1】:

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

    window2.windowLevel = UIWindowLevelNormal + 1;
    

    PS:

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

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

    【讨论】:

      【解决方案2】:

      只需使用UISplitViewController

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

      【讨论】:

      • 第二个导航控制器需要有一个自定义框架,所以很遗憾,这不起作用。
      • 感谢您的建议。看了MGSplitViewController的代码,好像只有根视图有导航控制器。
      【解决方案3】:

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

      【讨论】:

      • 你能扩展一下吗?我遇到了同样的问题。 clipsToBounds 在新窗口上?
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        试试这个代码...

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

        【讨论】:

          猜你喜欢
          • 2016-05-18
          • 1970-01-01
          • 2022-01-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-03-16
          • 2015-10-31
          • 1970-01-01
          相关资源
          最近更新 更多