您好我已经创建了一个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()更多推荐
发布评论