8

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];
Community
  • 1
  • 1
andreas
  • 7,844
  • 9
  • 51
  • 72

4 Answers4

9

in AppDelegate file

self.window.windowLevel = UIWindowLevelStatusBar;

or in any other class

[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;

and if you want to make status bar to be on the top again u can set

self.window.windowLevel = UIWindowLevelNormal;
Mihriban Minaz
  • 3,043
  • 2
  • 32
  • 52
8

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.
tc.
  • 33,468
  • 5
  • 78
  • 96
  • Hello, this is brilliant! First I didn't believe it could work, but it actually did! Thanks a lot for saving me time and frustration! :-) – andreas Nov 23 '10 at 23:28
  • Which method did you end up using? – tc. Nov 24 '10 at 02:42
  • See my last post: I had to remove the command "statusWindow makeKeyAndVisible" and use "statusWindow.hidden = NO" instead. At the end of all code I added "window makeKeyAndVisible" so the app knows it has to show keyboard there. It worked! I hope Apple will accept this solution. – andreas Nov 25 '10 at 00:53
  • It doesn't use any "private APIs", so Apple is unlikely to reject it. It's a bit worrying that you can place a window above the status bar. OTOH, there's nothing stopping you from hiding the "real" status bar and displaying a clone, so it's not really a big security risk either. – tc. Nov 27 '10 at 03:23
7

If other users want to implement this, here my solution:

// Create window
statusWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,0,320,20)];
statusWindow.windowLevel = UIWindowLevelStatusBar;
// Dont make the statusWindow keyWindow or the keyboard won't work!
// [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];

// Instead, add this:
[window makeKeyAndVisible]; // has to be main window of app
statusWindow.hidden = NO; // without this the statusWindow won't appear
andreas
  • 7,844
  • 9
  • 51
  • 72
  • 1
    This code doesn't seem to work. You're not adding `statusWindow` to your view hierarchy as far as I can tell. How do you get it to appear? – jbrennan Dec 02 '10 at 00:49
  • I declared window and statusWindow in my h-file and made synthesize in my m-file. Nothing else was done as far as I checked! Please note that my button is invisible and only at the top of the statusbar and will call the function goTop which needs to be declared. iPhone app rotation will also confuse this so you have to resize the button then. – andreas Dec 07 '10 at 23:44
  • This dosen't work for me neither, this code is at the AppDelegate? – JP Illanes Jun 11 '12 at 19:38
0

Try:

[self.view bringSubviewToFront:statusWindow];

I dont think its possible to bring a view in front of the status bar though.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • Unfortunately, I couldn't make it work. The only way to do it is by adding a new (which is normally not recommended!). – andreas Nov 23 '10 at 23:31