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

查看:523
本文介绍了将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添加到所有其他视图之上,包括StatusBar
即使不推荐第二个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的继承隐藏属性隐藏和显示窗口。

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没有superview你可以调用-bringSubviewToFront:on IIRC。

  • 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天全站免登陆