代码之家  ›  专栏  ›  技术社区  ›  iOS Lifee

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

  •  -2
  • iOS Lifee  · 技术社区  · 6 年前

    我想在状态栏中显示所有视图控制器都应该可见的视图

    例子: 我需要在状态栏中显示“没有Internet连接”,当我们连接到Internet后它会隐藏起来吗?

    2 回复  |  直到 6 年前
        1
  •  6
  •   LinusG.    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)
    

    如果你申报 statusBar 以及 noNetworkConnectionView 在全局范围内,您可以从任何位置访问它以动态显示或隐藏它。

    结果:

    result


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

        2
  •  0
  •   Mahendra    6 年前
    • 创建视图

      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()