PersuitOfPerfection
PersuitOfPerfection

Reputation: 1029

Position UIView above status bar

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?

Upvotes: 1

Views: 3157

Answers (2)

Ivan Stojkovic
Ivan Stojkovic

Reputation: 663

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

}];

}

Upvotes: 1

Tom
Tom

Reputation: 968

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.

Upvotes: 0

Related Questions