我正在iphone的状态栏上添加一个覆盖图。但是,当我在xcode 11.1上切换到IOS 13.1时,无法使用UIWindow类
在状态栏上显示它class PassTroughWindow: UIWindow {
var passTroughTag: Int?
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let hitView = super.hitTest(point, with: event)
if let passTroughTag = passTroughTag {
if passTroughTag == hitView?.tag {
return nil
}
}
return hitView
}
}
class ViewController: UIViewController {
var window: PassTroughWindow?
@IBAction func alertButtonPressed(_ sender: UIButton) {
print("Height : \(UIApplication.shared.statusBarFrame.height)")
print("Height : \(UIApplication.shared.statusBarFrame.maxY)")
let frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: UIApplication.shared.statusBarFrame.maxY)
let banner = UIView(frame: frame)
banner.layer.cornerRadius = 2.5
banner.backgroundColor = UIColor.init(red: 128.0/255.0, green: 161.0/255.0, blue: 193.0/255.0, alpha: 1.0)
let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: frame.width, height: frame.height))
label.textAlignment = .center
label.font = .boldSystemFont(ofSize: 10)
label.text = "No internet connection"
label.textColor = .black
banner.addSubview(label)
self.window = PassTroughWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = UIViewController()
self.window?.windowLevel = .statusBar + 1
self.window?.addSubview(banner)
self.window?.makeKeyAndVisible()
self.window?.alpha = 1.0
}
}
如果在IOS 13.1的Xcode 11.1中执行相同的代码,则在调试器()中给出此结果:-
在IOS 12.2的xcode 10.2中,它给了我这个结果
IOS 13.1中的最终结果是Xcode 11.1
IOS 12.2和xcode 10.2中的最终结果是
如何在IOS 13中的状态栏上显示叠加图?