在iOS 8中显示Apple Status Bar上方的UIView

Ale*_*der 1 screen statusbar ios swift

我知道有关于此问题的一些问题,但没有一个问题帮我解决了我的具体问题.我想在整个屏幕上方显示一个自定义模式,但我想保持Apple状态栏可见.对于模态,我使用一个UIView用于调光效果,另一个用于用户将与之交互的实际视图.然后将整个模态作为子视图添加到当前视图并转到前面.本质上,我试图复制UIAlertView的行为,除了自定义视图.这是我的一些代码:

var modalView:UIView = UIView(frame: self.view.window!.bounds)
modalView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalView.addSubview(customView)    

self.view.window!.addSubview(modalView)
modalView.window!.windowLevel = UIWindowLevelStatusBar + 1

self.bringSubviewToFront(modalView)
Run Code Online (Sandbox Code Playgroud)

以上customViewUIView用户与之交互的内容.

这很好用,但由于某种原因,即使状态栏的样式设置为LightContent,Apple状态栏上的文本也会消失.从代码中可以看出,我没有触摸状态栏.

我试图让状态栏上的文字像屏幕的其余部分一样暗淡,目前卡住了.有没有人对如何获得理想的行为有任何见解?

任何帮助将不胜感激!

Mat*_*ker 7

2017年12月更新•Swift 4•iOS 10.0+

我简单地尝试了接受的答案的编辑解决方案,但我不能轻松地为一个UIView横幅制作一个完整的viewController.但是,通过访问statusBarUIApplication,我能够将UIView添加为子视图statusBarView.这将把UIView放在状态栏的顶部.据我所知,这是一个相对稳定的解决方案,适用于最后几个iOS版本.

extension UIApplication {

    /// Returns the status bar UIView
    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}
Run Code Online (Sandbox Code Playgroud)

用法

let customBannerView = CustomStatusView(with: message)
UIApplication.shared.statusBarView?.addSubview(customBannerView)
Run Code Online (Sandbox Code Playgroud)


Ale*_*der 5

编辑(2015年11月16日)

我正在从以下帖子中复制我的答案:

这个答案在iOS 8.3+以及可能的iOS 8的早期版本中打破.我不建议任何人使用它,特别是因为它不能保证在未来的iOS版本中工作.

我的新解决方案使用a的标准presentViewController方法UIViewController.我初始化a UIViewController,添加我的自定义模态View作为子视图UIViewController,然后可选地View使用约束定位模态.奇迹般有效!


原始答案

遵循安娜的建议,我通过使用UIWindow而不是UIView.以下是我的新代码来实现它:

var modalWindow:UIWindow = UIWindow(frame: self.view.window!.bounds)

modalWindow.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.66)
modalWindow.hidden = false
modalWindow.windowLevel = (UIWindowLevelStatusBar + 1)

modalWindow.addSubview(customView)    

modalWindow.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

它现在与以前完全一样,除了Apple状态栏上的文本也变暗了.非常感谢您的帮助!