2010-11-23 72 views
7

我的目标是在iPhone应用程序顶部的状态栏上方绘制一个隐形按钮(尺寸为320 * 20像素)。在状态栏上方放置UIView或UIWindow

无论我怎么努力,事情是越野车:

  1. 例如,我试图创建一个新的观点。当我想将视图置于应用程序的顶部时,它总是在状态栏后面消失,而不是位于它的前面!

  2. 我发现#2另一个伟大的想法: Add UIView Above All Other Views, Including 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]; 

回答

8

在用于文档 - [一个UIWindow makeKeyAndVisible]:

这是一个方便的方法,使接收器的主窗口中,并显示在其他窗口的前面。您还可以使用UIView的继承隐藏属性来隐藏和显示窗口。

“关键窗口”是获取某些输入事件的窗口;非关键窗口中的文本字段可能不起作用。有两件事情可以做:

  • 电话[窗口makeKeyAndVisible]的关键窗口之后
  • statusWindow.hidden = NO代替(但我不明白为什么它会默认为隐藏)。我不认为你可以像“-makeKeyAndVisible”那样“将它显示在其他窗口的前面” Windows没有超级视图,您可以在IIRC上调用-bringSubviewToFront:。
+0

你好,这太棒了!首先,我不相信它可以工作,但实际上它确实!非常感谢您为我节省时间和挫折! :-) – andreas 2010-11-23 23:28:14

+0

你最终使用了哪种方法? – 2010-11-24 02:42:47

0

尝试:

[self.view bringSubviewToFront:statusWindow]; 

我不认为它可能带来视图状态栏的前面,虽然。

+0

不幸的是,我无法让它工作。唯一的方法是添加一个新的(通常不推荐!)。 – andreas 2010-11-23 23:31:47

7

如果其他用户要实现这一点,在这里我的解决方案:

// Create window 
statusWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0,0,320,20)]; 
statusWindow.windowLevel = UIWindowLevelStatusBar; 
// Dont make the statusWindow keyWindow or the keyboard won't work! 
// [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]; 

// Instead, add this: 
[window makeKeyAndVisible]; // has to be main window of app 
statusWindow.hidden = NO; // without this the statusWindow won't appear 
7

中的AppDelegate文件

self.window.windowLevel = UIWindowLevelStatusBar; 

或以任何其它类

[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar; 

,如果你想要使状态栏再次位于顶部,您可以设置

self.window.windowLevel = UIWindowLevelNormal;