Add multiple UIWindow

心已入冬 提交于 2019-12-23 20:27:42

问题


I am adding a new UIWIndow over another to display a view, but it is not showing anything and the screen just gets a little blurred. Here is the code:

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;

What am I doing wrong?


回答1:


I wanna do like you too.

@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;
}
.....

And I did it. I think you should retain your new UIWindows. (I'm using ARC so I defined 1 local var to retain it)

Good luck!




回答2:


Set windowLevel property to another value. I usually use:

topWindow.windowLevel = UIWindowLevelAlert + 1;



回答3:


Why are you trying to create a second window to overlay the main one?

From the Apple docs:

"A UIWindow object coordinates the presentation of one or more views on a screen. Most apps have only one window, which presents content on the main screen, but apps may have an additional window for content displayed on an external display."

You should be presenting your additional viewController modally or using "UIViewController containment". All within the one window.



来源:https://stackoverflow.com/questions/19315559/add-multiple-uiwindow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!