Putting a UIView or UIWindow above Statusbar

andreas picture andreas · Nov 23, 2010 · Viewed 15.9k times · Source

My goal is to draw an invisible button above the status bar on the top of my iPhone app (dimension 320*20 pixels).

No matter what I try, something is buggy:

  1. For example, I tried to create a new view. When I want to place the view on the top of my app, it always disappears behind the status bar instead of being in front of it!

  2. I found another great idea on Stackoverflow: Add UIView Above All Other Views, Including StatusBar Even if a second UIWindow isn't recommended, I tried to implement it. It worked as I wanted until the moment that I noticed a problem: the keyboard doesn't appear anymore when needed (for example when clicking in a textbox)!

How can I possibly fix this? Or is there a better approach to my problem? This is my code for creating the second window:

// Create window
statusWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,0,320,20)];
statusWindow.windowLevel = UIWindowLevelStatusBar;
[statusWindow makeKeyAndVisible];

// Create statusBarButton
statusBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonFrame2 = statusBarButton.frame;
buttonFrame2.size = CGSizeMake(320,20);
statusBarButton.frame = buttonFrame2;
[statusBarButton addTarget:self action:@selector(goTop) forControlEvents:UIControlEventTouchUpInside]; 

// Place button into the new window
[statusWindow addSubview:statusBarButton];

Answer

tc. picture tc. · Nov 23, 2010

In the documentation for -[UIWindow makeKeyAndVisible]:

This is a convenience method to make the receiver the main window and displays it in front of other windows. You can also hide and reveal a window using the inherited hidden property of UIView.

The "key window" is the one that gets certain input events; text fields in a non-key window might not work. There are two things you can do:

  • Call [window makeKeyAndVisible] on the old key window afterwards
  • Set statusWindow.hidden = NO instead (but I don't see why it would be hidden by default). I don't think you can "display it in front of other windows" like -makeKeyAndVisible does; windows don't have a superview you can call -bringSubviewToFront: on IIRC.