8

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

无论我尝试什么,都有问题:

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

  2. 我在 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];
4

4 回答 4

9

在 AppDelegate 文件中

self.window.windowLevel = UIWindowLevelStatusBar;

或在任何其他类别

[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;

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

self.window.windowLevel = UIWindowLevelNormal;
于 2014-06-05T13:59:55.613 回答
8

在 -[UIWindow makeKeyAndVisible] 的文档中:

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

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

  • 之后在旧键窗口上调用 [window makeKeyAndVisible]
  • 改为设置statusWindow.hidden = NO(但我不明白为什么它会默认隐藏)。我认为您不能像 -makeKeyAndVisible 那样“在其他窗口前显示它”;Windows 没有超级视图,您可以在 IIRC 上调用 -bringSubviewToFront:。
于 2010-11-23T04:29:40.883 回答
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
于 2010-11-23T23:30:18.867 回答
0

尝试:

[self.view bringSubviewToFront:statusWindow];

我认为它不可能在状态栏前面带来一个视图。

于 2010-11-23T02:24:20.933 回答