将 UIView 或 UIWindow 放置在 Statusbar 上方
我的目标是在 iPhone 应用程序顶部的状态栏上方绘制一个不可见的按钮(尺寸 320*20 像素)。
无论我尝试什么,都会出现问题:
例如,我尝试创建一个新视图。当我想将视图放置在应用程序顶部时,它总是消失在状态栏后面而不是在状态栏前面!
我在 Stackoverflow 上发现了另一个好主意: 将 UIView 添加到所有其他视图之上,包括 StatusBar 即使不推荐第二个 UIWindow,我也尝试实现它。它按照我想要的方式工作,直到我注意到一个问题:键盘在需要时不再出现(例如单击文本框时)!
我怎样才能解决这个问题?或者有更好的方法来解决我的问题吗?这是我创建第二个窗口的代码:
// 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];
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:
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!
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 AppDelegate 文件
或任何其他类
中,如果您想让状态栏再次位于顶部,您可以设置
in AppDelegate file
or in any other class
and if you want to make status bar to be on the top again u can set
在 -[UIWindow makeKeyAndVisible] 的文档中:
“关键窗口”是获取某些输入事件的窗口;非键窗口中的文本字段可能不起作用。您可以做两件事:
statusWindow.hidden = NO
(但我不明白为什么会这样)默认隐藏)。我不认为你可以像 -makeKeyAndVisible 那样“将其显示在其他窗口前面”; windows 没有超级视图,您可以在 IIRC 上调用 -bringSubviewToFront: 。In the documentation for -[UIWindow makeKeyAndVisible]:
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:
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.如果其他用户想实现这一点,这里是我的解决方案:
If other users want to implement this, here my solution:
尝试:
不过,我认为不可能在状态栏前面显示视图。
Try:
I dont think its possible to bring a view in front of the status bar though.