如何在iOS的状态栏中显示视图?

iOS*_*fee -2 ios swift

我想在状态栏中显示对所有View Controller都可见的视图

示例:我需要在状态栏中显示“没有Internet连接”,并且在我们获得Internet连接后它会隐藏吗?

Lin*_*rth 6

您可以将视图直接添加到状态栏:

// get the status bar
let statusBar = UIApplication.shared.value(forKey: "statusBarWindow") as? UIWindow

// create a subview & add it to the status bar
let subview = UIView(frame: CGRect(x: 20, y: 0, width: 20, height: 10))
subview.backgroundColor = .red
statusBar?.addSubview(subview)
statusBar?.bringSubview(toFront: subview)
Run Code Online (Sandbox Code Playgroud)

如果声明statusBarnoNetworkConnectionView,则可以从任何地方访问它以动态显示或隐藏它。

结果:

结果


免责声明:我不确定Apple是否会批准以这种方式修改状态栏的应用程序。