for sub in foregroundView.subviews {
if sub.isKind(of: NSClassFromString("UIStatusBarBatteryItemView")!){
print(sub)
sub.removeFromSuperview()
}
}
所有代碼
method1() 對應方法一
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() 對應方法二
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!)
}