尝试通过AppDelegate将子视图添加到UI的前端,但它无法正常工作。我有一个可达性通知程序,它应该在没有互联网连接的情况下从xib添加UIView。
App委托代码:
func reachabilityChanged(note: Notification) {
let reachability = note.object as! Reachability
if !reachability.isReachable {
print("APP: Network reachable")
if networkErrorView != nil {
networkErrorView.removeFromSuperview()
}
} else {
print("ERROR: Network not reachable")
networkErrorView = NetworkErrorView(frame: CGRect(x: 30, y: self.window!.frame.height / 4, width: self.window!.frame.width - 60, height: self.window!.frame.height / 2))
UIApplication.shared.keyWindow?.addSubview(networkErrorView)
UIApplication.shared.keyWindow?.bringSubview(toFront: networkErrorView)
print("View is showing")
}
}
通知会触发,我会在控制台上打印“View is showing”。我试过通过dispatchQueue.main.async
来调用它,但这只会导致无限循环崩溃应用程序。
由于
答案 0 :(得分:1)
private func dismissOfflineScreen() {
if let navigationVC = self.window?.rootViewController?.presentedViewController as? UINavigationController, navigationVC.viewControllers.first is OfflineViewController {
navigationVC.dismiss(animated: true, completion: nil)
}
}
private func presentOfflineScreen() {
let navigationVC = UINavigationController(rootViewController: OfflineViewController())
navigationVC.view.backgroundColor = UIColor.clear
navigationVC.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
self.window?.rootViewController?.present(navigationVC, animated: true)
}
答案 1 :(得分:0)
if let currentViewController = self.window?.rootViewController {
let screenSize = UIScreen.main.bounds
networkErrorView = NetworkErrorView(frame: CGRect(x: 30, y: (screenSize.height / 4), width: (screenSize.width - 60), height: (screenSize.height / 2)))
currentViewController.view.addSubview(networkErrorView)
currentViewController.view.bringSubview(toFront: networkErrorView)
}