【问题标题】:Add multiple UIWindow添加多个 UIWindow
【发布时间】:2013-10-11 10:10:48
【问题描述】:

我在另一个上添加了一个新的 UIWIndow 来显示一个视图,但它没有显示任何内容并且屏幕变得有点模糊。代码如下:

UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[topWindow setWindowLevel:UIWindowLevelNormal];

CGFloat statusBarHeight = [UIApplication sharedApplication].statusBarFrame.size.height;

UIViewController* viewController = [[UIViewController alloc] init];
UIView* overlay = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight,   viewController.view.frame.size.width, statusBarHeight - 1)];
[overlay setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[overlay setBackgroundColor:[UIColor whiteColor]];
[viewController.view addSubview:overlay];
[topWindow setRootViewController:viewController];

[topWindow setHidden:NO];
[topWindow setUserInteractionEnabled:NO];
[topWindow makeKeyAndVisible];

viewController = nil;

overlay = nil;

我做错了什么?

【问题讨论】:

  • 为什么要添加这行viewController = nil;
  • 删除最后两行也没有任何影响
  • 看看这里。它应该让你更接近于做像stackoverflow.com/questions/1189078/…这样的事情
  • 你增加了窗位吗? window.windowLevel = UIWindowLevelAlert + 1

标签: ios objective-c uiwindow


【解决方案1】:

我也想和你一样。

@implementation TAAppDelegate {
    UIWindow *win;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        win = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        win.rootViewController = [UIViewController new];
        win.rootViewController.view.backgroundColor = [UIColor whiteColor];
        win.rootViewController.view.alpha = 0.5;
        [win makeKeyAndVisible];
    });
    return YES;
}
.....

我做到了。我认为你应该保留你的新 UIWindows。 (我使用的是 ARC,所以我定义了 1 个本地变量来保留它)

祝你好运!

【讨论】:

    【解决方案2】:

    将 windowLevel 属性设置为另一个值。 我通常使用:

    topWindow.windowLevel = UIWindowLevelAlert + 1;
    

    【讨论】:

    • 这是很多库使用的方式。您可以在许多 Toast 库中看到。您也不需要调用 makeKeyAndVisible,只需创建它。以及为什么设置 [topWindow setHidden:NO]; ?
    【解决方案3】:

    为什么要创建第二个窗口来覆盖主窗口?

    来自 Apple 文档:

    “UIWindow 对象协调屏幕上一个或多个视图的呈现。大多数应用程序只有一个窗口,用于在主屏幕上呈现内容,但应用程序可能有一个额外的窗口用于在外部显示器上显示内容。”

    您应该以模态方式展示您的附加 viewController 或使用“UIViewController 包含”。都在一个窗口中。

    【讨论】:

    • 实际上我正在制作自定义视图,就像 iOS7 + iOS6 支持的 UIAlertView 一样,并且需要在所有其他视图上显示它。我尝试在我的单个视图上添加,但它显示在错误的方向上,因为我在 rootviewcontroller 上显示。所以我的下一个解决方案是添加一个新窗口并将我的自定义视图作为 rootview,以便它可以获取旋转事件。
    • @ghazi_jaffary 如果你做得正确并使用 viewController 显示你的视图,那么它可以处理旋转。
    • 我无法将新的 ViewController 添加为 root 并且只有 root 可以有旋转事件。在那种情况下创建一个新的 UIWindow 不是正确的解决方案???
    • 不,如果您将视图控制器正确添加为子视图控制器,它们会从根视图控制器接收旋转事件。 developer.apple.com/library/ios/featuredarticles/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多