将 UIView 或 UIWindow 放置在 Statusbar 上方

发布于 2024-10-03 17:21:11 字数 1076 浏览 1 评论 0原文

我的目标是在 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];

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. 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!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

屋檐 2024-10-10 17:21:11

在 AppDelegate 文件

self.window.windowLevel = UIWindowLevelStatusBar;

或任何其他类

[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;

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

self.window.windowLevel = UIWindowLevelNormal;

in AppDelegate file

self.window.windowLevel = UIWindowLevelStatusBar;

or in any other class

[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;

and if you want to make status bar to be on the top again u can set

self.window.windowLevel = UIWindowLevelNormal;
帅气称霸 2024-10-10 17:21:11

在 -[UIWindow makeKeyAndVisible] 的文档中:

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

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

  • 键窗口上调用[window makeKeyAndVisible],然后
  • 设置statusWindow.hidden = NO(但我不明白为什么会这样)默认隐藏)。我不认为你可以像 -makeKeyAndVisible 那样“将其显示在其他窗口前面”; windows 没有超级视图,您可以在 IIRC 上调用 -bringSubviewToFront: 。

In the documentation for -[UIWindow makeKeyAndVisible]:

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:

  • 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.
没企图 2024-10-10 17:21:11

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

// 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

If other users want to implement this, here my solution:

// 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
风启觞 2024-10-10 17:21:11

尝试:

[self.view bringSubviewToFront:statusWindow];

不过,我认为不可能在状态栏前面显示视图。

Try:

[self.view bringSubviewToFront:statusWindow];

I dont think its possible to bring a view in front of the status bar though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文