新的UIWindow将无法显示

时间:2014-07-01 19:38:12

标签: ios objective-c uiwindow

我正在尝试创建一个新的UIWindow来覆盖整个屏幕,包括状态栏。当按下按钮时我会显示它,但它只是在屏幕上快速闪烁并消失。我做错了什么?

MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:@"MenuTableViewController" bundle:nil];

        UIWindow *menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
        menuWindow.backgroundColor = [UIColor clearColor];
        menuWindow.windowLevel = UIWindowLevelStatusBar;
        menuWindow.rootViewController = menu;
        [menuWindow addSubview:menu.view];
        [menuWindow makeKeyAndVisible];
        menuWindow.hidden = NO;

1 个答案:

答案 0 :(得分:2)

我通过将@property (nonatomic, strong) UIWindow *menuWindow;添加到我的头文件中来实现它。这是我的showMenu和closeMenu方法的样子。

- (void)showMenu
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

    UIButton *closeMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeMenuButton setFrame:CGRectMake(250, 10, 50, 50)];
    [closeMenuButton setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [closeMenuButton addTarget:self action:@selector(closeMenu) forControlEvents:UIControlEventTouchUpInside];

    blurredView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
    [blurredView setBarStyle:UIBarStyleBlack];

    MenuTableViewController *menu = [[MenuTableViewController alloc]initWithNibName:@"MenuTableViewController" bundle:nil];
    menu.view.frame = CGRectMake(0, 30, screenWidth, screenHeight - 50);

    menuWindow = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    menuWindow.backgroundColor = [UIColor clearColor];
    menuWindow.windowLevel = UIWindowLevelStatusBar;
    menuWindow.rootViewController = menu;
    [menuWindow addSubview:blurredView];
    [blurredView addSubview:closeMenuButton];
    [blurredView addSubview:menu.view];
    [menuWindow makeKeyAndVisible];
}

- (void)closeMenu
{
    menuWindow.hidden = YES;
    menuWindow = nil;
}
相关问题