我想让UIView坐在状态栏的顶部,但目前没有成功。我尝试过以下方法:
view.window?.windowLevel = UIWindowLevelStatusBar + 1 // view being a UIView() object
我也尝试过:
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()
在所有情况下,状态栏仍位于顶部。有人有这个在Swift工作吗?
答案 0 :(得分:1)
这是我的简单解决方案(你也可以使用MTStatusBarOverlay - 检查git hub)。
首先初始化窗口。在主窗口上设置makeKeyAndVisible后执行此操作。
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;
显示消息
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;
}];
}];
}
答案 1 :(得分:0)
为什么不隐藏状态栏?然后你可以把视图放在顶部。请参阅How to hide iOS status bar。
我认为可以用另一个视图覆盖状态栏,因为默认情况下所有内容都位于状态栏后面。