Swift 通过 appdelegate 将视图添加到前面
Swift add view to front via appdelegate
尝试通过 AppDelegate 将子视图添加到 UI 的前面,但它不起作用。我有一个可达性通知器,当没有互联网连接时,它应该从 xib 添加 UIView。
应用委托代码:
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
调用它,但这只会导致无限循环导致应用程序崩溃。
谢谢
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)
}
在其他尝试访问当前 viewController :
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)
}
尝试通过 AppDelegate 将子视图添加到 UI 的前面,但它不起作用。我有一个可达性通知器,当没有互联网连接时,它应该从 xib 添加 UIView。
应用委托代码:
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
调用它,但这只会导致无限循环导致应用程序崩溃。
谢谢
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)
}
在其他尝试访问当前 viewController :
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)
}