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

问题描述 投票:1回答:2

我知道有关于此问题的一些问题,但没有一个问题对我的具体问题有帮助。我想在整个屏幕上方显示一个自定义模式,但我想保持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)

在上面,customView是用户与之交互的UIView

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

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

任何帮助将不胜感激!

ios swift screen statusbar
2个回答
6
投票

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

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

extension UIApplication {

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

}

用法

let customBannerView = CustomStatusView(with: message)
UIApplication.shared.statusBarView?.addSubview(customBannerView)

5
投票

编辑(2015年11月16日)

我在下面的this帖子中复制我的答案:

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

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


原始答案

按照Anna的建议,我通过使用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()

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

© www.soinside.com 2019 - 2024. All rights reserved.