1

I am trying to get a UIView to sit on top of the status bar, but currently have had no success. I have tried the following:

view.window?.windowLevel = UIWindowLevelStatusBar + 1 // view being a UIView() object

I have also tried:

let win: UIWindow = UIWindow( )
win.frame = CGRect( x: 0, y: 0, width: 100, height: Display.height )
win.windowLevel = UIWindowLevelStatusBar + 1
win.hidden = false
win.backgroundColor = UIColor.blueColor()
win.makeKeyAndVisible()

In all cases the status bar is still on top. Has anyone got this to work in Swift yet?

PersuitOfPerfection
  • 1,009
  • 1
  • 15
  • 28

2 Answers2

1

This is my simple solution (you can also use MTStatusBarOverlay - check git hub).

first initialise the window. Do that after you set makeKeyAndVisible on your main window.

  self.syncMessageWindow = [[UIWindow alloc] initWithFrame:messageRect];
  self.syncMessageWindow.clipsToBounds = YES;
  self.syncMessageWindow.frame = messageRect;
  self.syncMessageWindow.windowLevel = UIWindowLevelStatusBar+1.f;
  self.syncMessageWindow.alpha = 1.f;

  self.syncMessageView = [[UIView alloc] initWithFrame:messageRect];
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 2, messageRect.size.width-20, 16)];
  [label setTag:100];
  [label setTextAlignment:NSTextAlignmentCenter];
  [label setFont:[UIFont fontWithName:@"HelveticaNeue" size:12.0]];
  [label setTextColor:[UIColor whiteColor]];
  [self.syncMessageView addSubview:label];
  [self.syncMessageWindow addSubview:self.syncMessageView];
  [self.syncMessageWindow setRootViewController:[UIApplication sharedApplication].delegate.window.rootViewController];
  [self.syncMessageWindow makeKeyAndVisible];
  self.syncMessageWindow.hidden = YES;

than show the message

if(self.syncMessageWindow.hidden == YES) {
self.syncMessageWindow.hidden = NO;
[self.syncMessageView setBackgroundColor:color];
[self.syncMessageView setAlpha:1.0];
[self.syncMessageView setFrame:CGRectMake(0, -20, self.syncMessageView.frame.size.width, self.syncMessageView.frame.size.height)];
[self.syncMessageWindow setWindowLevel:UIWindowLevelStatusBar+1];
[((UILabel*)[self.syncMessageView viewWithTag:100]) setText:message];
[UIView animateWithDuration:.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
  self.syncMessageView.frame = CGRectMake(0, 0, self.syncMessageView.frame.size.width, self.syncMessageView.frame.size.height);
} completion:^(BOOL finished) {
  [UIView animateWithDuration:.3 delay:2.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
    self.syncMessageView.frame = CGRectMake(0, -20, self.syncMessageView.frame.size.width, self.syncMessageView.frame.size.height);
  } completion:^(BOOL finished) {
    [self.syncMessageWindow setWindowLevel:UIWindowLevelNormal];
    self.syncMessageWindow.hidden = YES;
  }];

}];

}

Ivan Stojkovic
  • 653
  • 1
  • 8
  • 15
0

Why don't you hide the status bar? Then you can put the view at the top. See How to hide iOS status bar.

I don't think it is possible to cover the status bar with another view, because all content is behind the status bar by default.

Community
  • 1
  • 1
Tom
  • 874
  • 7
  • 13
  • Hey Tom. In certain screens my app needs to use a status bar. I only hide it when a slide out menu is shown (so it doesn't get covered at the top by the status bar). The problem with this approach is that the scene i change to (instantaneously) then shows the navigation bar (slide down anim) which is fine, but then my programatically created UIToolbar no longer covers the content behind the status bar, and yes I am overriding the positionForBar method, and setting it's return value to UIBarPosition.TopAttached. – PersuitOfPerfection Nov 06 '14 at 14:18
  • I don't know if I understand you correctly. But this seems like a bug. However, you can try to embed your view in a NavigationController and use the navigation bar of this controller instead of your custom UIToolbar. – Tom Nov 06 '14 at 14:28
  • You did read this page? https://developer.apple.com/library/ios/qa/qa1797/_index.html – Tom Nov 06 '14 at 14:34
  • yeah I have seen that. Looks like using a UINavigationBar will work with this situation better than a UIToolbar. I'll try that. Thanks for your help thus far. I'll report back after getting it set up – PersuitOfPerfection Nov 06 '14 at 15:48
  • using a UINavigation bar did the trick, I can keep my old behaviour of hiding and showing the status bar, but without it messing up my navbar (like it did when I was using a UIToolbar), thanks for the help. I have accepted your answer. – PersuitOfPerfection Nov 06 '14 at 16:33
  • The accepted answer is not answering the question. check out @Ivan Stojkovic answer. – Vincenzo Jun 05 '16 at 10:34