根据控制器中的条件,为UIActivityIndicator创建一个Utility函数来显示和隐藏(Create a Utility function for UIActivityIndicator

编程入门 行业动态 更新时间:2024-10-27 08:27:00
根据控制器中的条件,为UIActivityIndicator创建一个Utility函数来显示和隐藏(Create a Utility function for UIActivityIndicator to show and hide depending on the condition in controller)

您好我已经创建了一个swift文件,其中我编写了一些实用程序函数,我在多个控制器中使用它。 我还为UIActivityIndicator编写了函数。 但不知何故,它没有按预期工作。

这是我的功能

static func showIndicatorView(backgroundView: UIView,controller: UIViewController)->UIActivityIndicatorView{ let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) let backgroundView = UIView() backgroundView.layer.cornerRadius = 05 backgroundView.clipsToBounds = true backgroundView.opaque = false backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6) loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray loadingIndicator.color = UIColor.whiteColor() loadingIndicator.startAnimating() let loadingLabel = UILabel() loadingLabel.text = "Loading..." loadingLabel.textColor = UIColor.whiteColor() let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ]) loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height) loadingLabel.center.y = loadingIndicator.center.y backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50) backgroundView.center = controller.view.center; controller.view.addSubview(backgroundView) backgroundView.addSubview(loadingIndicator) backgroundView.addSubview(loadingLabel) return loadingIndicator }

我在控制器中执行此操作以显示和隐藏指示器

显示 Utility.showIndicatorView(backgroundView,controller:self).startAnimating()

隐藏

Utility.showIndicatorView(backgroundView, controller: self).startAnimating()

有时UiIndicatorView背景不会从控制器中删除。 请检查我的代码,让我知道如何在一两行显示和隐藏uiindicator

Hello I have created a swift file in which I wrote some utility functions which I used in multiple controllers. I also wrote function for UIActivityIndicator. But somehow its not working as expected.

Here is my function

static func showIndicatorView(backgroundView: UIView,controller: UIViewController)->UIActivityIndicatorView{ let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) let backgroundView = UIView() backgroundView.layer.cornerRadius = 05 backgroundView.clipsToBounds = true backgroundView.opaque = false backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6) loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray loadingIndicator.color = UIColor.whiteColor() loadingIndicator.startAnimating() let loadingLabel = UILabel() loadingLabel.text = "Loading..." loadingLabel.textColor = UIColor.whiteColor() let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ]) loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height) loadingLabel.center.y = loadingIndicator.center.y backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50) backgroundView.center = controller.view.center; controller.view.addSubview(backgroundView) backgroundView.addSubview(loadingIndicator) backgroundView.addSubview(loadingLabel) return loadingIndicator }

I am doing this in controllers in order to show and hide the indicator

show Utility.showIndicatorView(backgroundView, controller: self).startAnimating()

hide

Utility.showIndicatorView(backgroundView, controller: self).startAnimating()

Sometimes UiIndicatorView background doesn't remove from the controller. Please check my code and let me know how can I show and hide the uiindicator in one or two lines

最满意答案

为加载视图创建一个类,并添加函数以显示和隐藏视图,如下所示:

import UIKit class LoadingView: UIView { override init (frame : CGRect) { super.init(frame : frame) let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) let backgroundView = UIView() backgroundView.layer.cornerRadius = 05 backgroundView.clipsToBounds = true backgroundView.opaque = false backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6) loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray loadingIndicator.color = UIColor.whiteColor() loadingIndicator.startAnimating() let loadingLabel = UILabel() loadingLabel.text = "Loading..." loadingLabel.textColor = UIColor.whiteColor() let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ]) loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height) loadingLabel.center.y = loadingIndicator.center.y backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50) backgroundView.center = self.center; self.addSubview(backgroundView) backgroundView.addSubview(loadingIndicator) backgroundView.addSubview(loadingLabel) } convenience init () { self.init(frame:UIScreen.mainScreen().bounds) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func showLoadingView() { if let rootViewController = UIApplication.topViewController() { rootViewController.view.addSubview(self) self.bringSubviewToFront(rootViewController.view) UIApplication.sharedApplication().networkActivityIndicatorVisible = true } } func hideLoadingView() { UIApplication.sharedApplication().networkActivityIndicatorVisible = false self.removeFromSuperview() } } // Get the visible ViewController extension UIApplication { class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { if let nav = base as? UINavigationController { return topViewController(nav.visibleViewController) } if let tab = base as? UITabBarController { let moreNavigationController = tab.moreNavigationController if let top = moreNavigationController.topViewController where top.view.window != nil { return topViewController(top) } else if let selected = tab.selectedViewController { return topViewController(selected) } } if let presented = base?.presentedViewController { return topViewController(presented) } return base } }

然后在ViewController中初始化加载视图。

let lv = LoadingView() lv.showLoadingView() lv.hideLoadingView()

Create a class for your loading view and add functions to show and hide the view like this:

import UIKit class LoadingView: UIView { override init (frame : CGRect) { super.init(frame : frame) let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) let backgroundView = UIView() backgroundView.layer.cornerRadius = 05 backgroundView.clipsToBounds = true backgroundView.opaque = false backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6) loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray loadingIndicator.color = UIColor.whiteColor() loadingIndicator.startAnimating() let loadingLabel = UILabel() loadingLabel.text = "Loading..." loadingLabel.textColor = UIColor.whiteColor() let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ]) loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height) loadingLabel.center.y = loadingIndicator.center.y backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50) backgroundView.center = self.center; self.addSubview(backgroundView) backgroundView.addSubview(loadingIndicator) backgroundView.addSubview(loadingLabel) } convenience init () { self.init(frame:UIScreen.mainScreen().bounds) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func showLoadingView() { if let rootViewController = UIApplication.topViewController() { rootViewController.view.addSubview(self) self.bringSubviewToFront(rootViewController.view) UIApplication.sharedApplication().networkActivityIndicatorVisible = true } } func hideLoadingView() { UIApplication.sharedApplication().networkActivityIndicatorVisible = false self.removeFromSuperview() } } // Get the visible ViewController extension UIApplication { class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? { if let nav = base as? UINavigationController { return topViewController(nav.visibleViewController) } if let tab = base as? UITabBarController { let moreNavigationController = tab.moreNavigationController if let top = moreNavigationController.topViewController where top.view.window != nil { return topViewController(top) } else if let selected = tab.selectedViewController { return topViewController(selected) } } if let presented = base?.presentedViewController { return topViewController(presented) } return base } }

Then init your loading view in your ViewController.

let lv = LoadingView() lv.showLoadingView() lv.hideLoadingView()

更多推荐

本文发布于:2023-07-15 11:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1112862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:创建一个   函数   器中   条件   UIActivityIndicator

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!