2014-07-01 37 views
0

我想創建一個新的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; 

回答

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