隱藏狀態(tài)欄電池圖標(UIStatusBar UIStatusBarBatteryItemView iOS Swift4.0)

但是如何修改狀態(tài)欄中電池圖標呢?

  • 注意點 Application -> statusBar -> 電池視圖(UIStatusBarBatteryItemView)

方法1

  • 通過遍歷子視圖的方式,查找到電池視圖,(默認知道電池視圖對應(yīng)的view名字是 ‘UIStatusBarBatteryItemView’)
  • 難點一 UIApplication 不是一個UIView, 所以要通過valueForKey方式查找到statusBar, 由此引發(fā)另一個難點
  • 難點二 怎么知道statusBar 對應(yīng)的key, 可以通過class_copyIvarList 和 ivar_getName, String.init(utf8String: name!) 找到statusBar 對應(yīng)的key,也就是 '_statusBar'
  • 難點三 通過遍歷查找(遞歸方法找到)如下, 直接移除即刻, 方法要寫在viewDidLayoutSubviews里面

代碼如下

      func getAllSubViews(view: UIView) {
          if view.subviews.count == 0 {
             return
          }
          for sub in view.subviews {
              //print(sub)
              if sub.isKind(of: NSClassFromString("UIStatusBarBatteryItemView")!) {
                 sub.removeFromSuperView()
              }
              getAllSubViews(view: sub)
          }
      }

方法2

  • 一路下來通過key value 分別查找到對子視圖的key然后通過valueforkey方式來找到對應(yīng)的view,
  • 分別對于應(yīng)的key是 Application -> _statusBar -> _foregroundView
  • 到foregroundView之后查找UIStatusBarBatteryItemView對應(yīng)的key時發(fā)現(xiàn)查找不到,是一個隱式變量,這個時候最后一步還是要遍歷子視圖才行

代碼如下

        for sub in foregroundView.subviews {

            if sub.isKind(of: NSClassFromString("UIStatusBarBatteryItemView")!){
                print(sub)
                sub.removeFromSuperview()
            }
        }
   

所有代碼

method1() 對應(yīng)方法一

 func method1(){

        let statusBar = UIApplication.shared.value(forKey: "_statusBar") as! UIView
        let foregroundView = statusBar.value(forKey: "_foregroundView") as! UIView
        for sub in foregroundView.subviews {

            if sub.isKind(of: NSClassFromString("UIStatusBarBatteryItemView")!){
                sub.removeFromSuperview()
            }
        }

    }

method2() 對應(yīng)方法二

    func method2() {
        //查找statusBar的子視圖,并隱藏電池視圖
        func hideBatteryViewFor(view: UIView) {
            if view.subviews.count == 0 {
                return
            }
            for sub in view.subviews {
                //print(sub)
                if sub.isKind(of: NSClassFromString("UIStatusBarBatteryItemView")!) {
                    sub.removeFromSuperview()
                }
                hideBatteryViewFor(view: sub)
            }
        }

        let statusBar = UIApplication.shared.value(forKey: "_statusBar") as! UIView
        hideBatteryViewFor(view: statusBar)

    }
#方便方法 , 獲取變量和屬性
    func getIvarFrom(other: AnyClass){

        var count: UInt32 = 0
        let ivarList = class_copyIvarList(other, &count)
        for i in 0..<count{
            let name = ivar_getName(ivarList![Int(i)])
            print(NSStringFromClass(other) + String.init(utf8String: name!)!)
        }

    }
    //獲取父視圖
    func getSuperView(view: UIView) {

        if view.superview == UIApplication.shared || view.superview == nil {
            return
        }
        print("superviEW")
        print(view)
        getSuperView(view: view.superview!)
    }

github代碼地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容