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

时间:2018-06-05 11:57:32

标签: ios swift

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

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

2 个答案:

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

如果您在全球范围内声明statusBarnoNetworkConnectionView,则可以从任意位置访问它以动态显示或隐藏它。

结果:

result

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

答案 1 :(得分:0)

  • 创建视图

    let statusView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 40))
    statusView.tag = 1101 //used to get view whenever required
    statusView.backgroundColor = UIColor.red.withAlphaComponent(0.6)
     //set other properties
    
  • 在窗口显示视图

    //show on window, will be visible in all view controller
    UIApplication.shared.delegate?.window??.addSubview(statusView)
    UIApplication.shared.delegate?.window??.bringSubview(toFront: statusView)
    
  • 隐藏视图

    //to hide that view
    UIApplication.shared.delegate?.window??.viewWithTag(1101)?.removeFromSuperview()
    
相关问题