gpt4 book ai didi

ios - Swift 通过 appdelegate 将 View 添加到前面

转载 作者:行者123 更新时间:2023-11-28 15:05:33 25 4
gpt4 key购买 nike

尝试通过 AppDelegate 将 subview 添加到 UI 的前面,但它不起作用。我有一个可达性通知器,它应该在没有互联网连接时从 xib 添加 UIView。

应用委托(delegate)代码:

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)
}

关于ios - Swift 通过 appdelegate 将 View 添加到前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com