将 UIView 或 UIWindow 放在状态栏上方 [英] Putting a UIView or UIWindow above Statusbar

查看:23
本文介绍了将 UIView 或 UIWindow 放在状态栏上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在我的 iPhone 应用程序顶部的状态栏上方绘制一个不可见的按钮(尺寸为 320*20 像素).

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. 例如,我尝试创建一个新视图.当我想将视图放在我的应用程序顶部时,它总是消失在状态栏后面而不是在它前面!

  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!

我在 Stackoverflow 上发现了另一个好主意:在所有其他视图之上添加 UIView,包括状态栏即使不推荐使用第二个 UIWindow,我也尝试实现它.它按我的意愿工作,直到我注意到一个问题:键盘在需要时不再出现(例如在单击文本框时)!

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

推荐答案

在 -[UIWindow makeKeyAndVisible] 的文档中:

In the documentation for -[UIWindow makeKeyAndVisible]:

这是一种使接收器成为主窗口并显示在其他窗口前面的便捷方法.您还可以使用 UIView 继承的 hidden 属性隐藏和显示窗口.

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:

  • 之后在键窗口上调用[window makeKeyAndVisible]
  • 改为设置 statusWindow.hidden = NO (但我不明白为什么默认情况下它会被隐藏).我不认为你可以像 -makeKeyAndVisible 那样在其他窗口前显示它";Windows 没有超级视图,您可以在 IIRC 上调用 -bringSubviewToFront:.
  • 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.

这篇关于将 UIView 或 UIWindow 放在状态栏上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆