长期倾听者,第一次来电..
我正在尝试向我的应用程序添加一个横幅,该横幅将在没有网络连接时显示。我的应用程序使用大约 9 个 View Controller ,因此我认为 UIViewcontroller 的扩展将是明智的起点。我有一些逻辑来检查网络的运行状况,然后根据失败,它驱动NotificationCenter.post在当前显示器上显示viewController。此代码驻留在 UIViewController 扩展中,用于显示屏幕 - 它似乎起作用:
func showNoNetworkBanner()
{
if let theMask = parent?.view.viewWithTag(666) {
return // already setted..
} else {
let maskView = UIView()
maskView.tag = 666
maskView.backgroundColor = UIColor.lightGray
let vc = self.storyboard?.instantiateViewController(withIdentifier: "vc_NotNetworkWarning") as! vc_NotNetworkWarning
maskView.addSubview(vc.view)
parent?.view.addSubview(maskView)
}
}
另一方面,当 View Controller 出现时, View Controller 内会运行一些代码来查看运行状况是否正常。如果健康状况良好, View Controller 就会消失。在 View Controller 中,我几乎尝试了所有方法。当我使用这段代码时, View Controller 永远不会消失。
let vc = parent?.view.viewWithTag(666)
vc?.removeFromSuperview()
我尝试过的另一个变体是使用 .removeFrom ParentViewController() 并且 ViewController 确实消失了,但是我调用它的代码不起作用,因为 viewWithTag(666) 总是返回 true。
self.removeFromParentViewController()
有人可以帮我吗?有没有更好的方法在任何 View Controller 上显示单个“横幅”?
提前致谢!
最佳答案
晚上好。
根据您的问题,我假设您的网络连接检查正常工作。我将通过以下方式显示横幅:
使用包含以下代码的新 .swift 文件创建横幅:
class BannerViewLauncher: NSObject { var viewController: UIViewController? lazy var bannerView : UIView = { let view = UIView(frame: .zero) view.backgroundColor = .lightGray return view }() //create more UIelements if you want to design the banner further. don't forget to add them as subviews to the bannerView and give them constraints. func showNoNetworkBanner() { if let window = UIApplication.shared.keyWindow { window.addSubview(bannerView) //define your desired size here (y position is out of view so it is off view by default, the y position will be changed when you call the showNoNetworkBanner() method. bannerView.frame = CGRect(x: 0, y: -100, width: window.frame.width, height: 100) //change withDuration if you want it to display slower or faster, just change all of them to the same for it so look good UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseInOut, animations: { self.bannerView.frame = CGRect(x: 0, y: -100, width: window.frame.width, height: 100) }, completion: nil) } } func hideNoNetworkBanner() { if let window = UIApplication.shared.keyWindow { UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseIn, animations: { self.bannerView.frame = CGRect(x: 0, y: -100, width: window.frame.width, height: 100) }, completion: nil) } } }
在 ViewController 中,您希望显示
BannerViewLauncher
的这个(一个或多个)子类,并在 NoNetwork 观察者方法触发时调用该方法。lazy var bannerViewLauncher : BannerViewLauncher = { let launcher = BannerViewLauncher() launcher.viewController = self return launcher }() //call the following when you want the banner to display bannerViewLauncher.showNoNetworkBanner() //call this when you want to hide the banner bannerViewLauncher.hideNoNetworkBanner()
希望这能回答您的问题。
关于ios - 以编程方式显示和隐藏 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831661/