代码之家  ›  专栏  ›  技术社区  ›  andreas

在状态栏上方放置UIView或UIWindow

  •  7
  • andreas  · 技术社区  · 14 年前

    我的目标是在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 回复  |  直到 7 年前
        1
  •  8
  •   tc.    14 年前

    在-[UIWindow makeKeyAndVisible]的文档中:

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

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

    • 古老的 之后的关键窗口
    • statusWindow.hidden = NO 相反(但我不明白为什么它会被默认隐藏)。我认为你不能像makeKeyAndVisible那样“在其他窗口前显示它”;在IIRC上,窗口没有可以调用的超视图-bringSubviewToFront。
        2
  •  8
  •   Mihriban Minaz Andy Davies    10 年前

    在AppDelegate文件中

    self.window.windowLevel = UIWindowLevelStatusBar;
    

    或者在其他班级

    [AppDelegate appDelegate].window.windowLevel = UIWindowLevelStatusBar;
    

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

    self.window.windowLevel = UIWindowLevelNormal;
    
        3
  •  7
  •   andreas    14 年前

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

    // 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
    
        4
  •  0
  •   WrightsCS    14 年前

    尝试:

    [self.view bringSubviewToFront:statusWindow];
    

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