将UIView或UIWindow放在状态栏上方

8
我的目标是在iPhone应用程序顶部的状态栏上绘制一个不可见的按钮(尺寸为320*20像素)。
无论我尝试什么,都会出现一些问题:
1. 例如,我尝试创建一个新的视图。当我想将视图放在我的应用程序顶部时,它总是消失在状态栏后面而不是在其前面!
2. 我在Stackoverflow上找到了另一个好主意: 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];
4个回答

9

在AppDelegate文件中

self.window.windowLevel = UIWindowLevelStatusBar;

或者在任何其他类中。
[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;

如果您希望将状态栏再次放置在顶部,您可以进行以下设置:

self.window.windowLevel = UIWindowLevelNormal;

8

在-[UIWindow makeKeyAndVisible]的文档中:

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

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

  • 然后在关键窗口上调用[window makeKeyAndVisible]
  • 改为设置statusWindow.hidden = NO(但我不知道为什么默认情况下会被隐藏)。我认为您不能像-makeKeyAndVisible那样“将其显示在其他窗口前面”;Windows没有父视图可在其上调用-bringSubviewToFront: IIRC。

你好,这太棒了!一开始我不相信它能够运行,但它确实做到了!非常感谢你为我节省时间和挫折! :-) - andreas
你最终使用了哪种方法? - tc.
请看我的上一篇帖子:我不得不删除命令“statusWindow makeKeyAndVisible”,并改用“statusWindow.hidden = NO”。在所有代码的末尾,我添加了“window makeKeyAndVisible”,这样应用程序就知道它必须在那里显示键盘。它奏效了!我希望苹果能接受这个解决方案。 - andreas
它不使用任何“私有API”,因此苹果不太可能拒绝它。你可以将一个窗口放在状态栏上方,这有点令人担忧。另一方面,没有任何阻止你隐藏“真正”的状态栏并显示一个克隆,因此它实际上并不是一个很大的安全风险。 - tc.

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

1
这段代码似乎无法正常工作。据我所知,您没有将 statusWindow 添加到视图层次结构中。您如何让它出现? - jbrennan
我在h文件中声明了window和statusWindow,并在m文件中进行了synthesize。据我所知,除此之外没有进行其他操作!请注意,我的按钮是不可见的,只在状态栏顶部,并将调用需要声明的goTop函数。iPhone应用程序旋转也会混淆这一点,因此您必须重新调整按钮大小。 - andreas
这对我也不起作用,这段代码在AppDelegate吗? - JP Illanes

0

尝试:

[self.view bringSubviewToFront:statusWindow];

我认为不可能将视图置于状态栏前面。


很遗憾,我无法让它工作。唯一的方法是添加一个新的(这通常不被推荐!)。 - andreas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接