将UIView或UIWindow置于状态栏上方

时间:2010-11-23 01:59:43

标签: iphone uiview keyboard statusbar uiwindow

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

无论我尝试什么,都会出现问题:

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

  2. 我在Stackoverflow上发现了另一个好主意: Add UIView Above All Other Views, Including StatusBar 即使不推荐第二个UIWindow,我也尝试实现它。它直到我注意到一个问题时才按照我想要的方式工作:键盘在需要时不再出现(例如在文本框中单击时)!

  3. 我怎么可能解决这个问题?或者有更好的解决方法吗?这是我创建第二个窗口的代码:

    // 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 个答案:

答案 0 :(得分:8)

在 - [UIWindow makeKeyAndVisible]的文档中:

  

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

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

  • 之后在键窗口中调用[window makeKeyAndVisible]
  • 设置statusWindow.hidden = NO代替(但我不明白为什么默认情况下会隐藏它)。我不认为你可以“在其他窗口前显示它”,如-makeKeyAndVisible; windows没有superview你可以调用-bringSubviewToFront:on IIRC。

答案 1 :(得分:8)

在AppDelegate文件中

self.window.windowLevel = UIWindowLevelStatusBar;

或任何其他类

[AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;

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

self.window.windowLevel = UIWindowLevelNormal;

答案 2 :(得分: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

答案 3 :(得分:0)

尝试:

[self.view bringSubviewToFront:statusWindow];

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