如何从超级视图中删除以编程方式创建的UIView

时间:2019-04-24 10:17:46

标签: swift

我想在从API调用加载内容的同时实现加载叠加层,但是当我关闭视图时,我没有成功。

func viewLoading(show:Bool, boxView: UIView, error: Bool, errorMessage: String){
    let myNewView=UIView(frame: CGRect(x: 0, y: 0, width: boxView.frame.width, height: boxView.frame.height))

    if show{
        // Change UIView background colour
        myNewView.backgroundColor = UIColor.black.withAlphaComponent(0.75)
        myNewView.isOpaque = false
        // Add rounded corners to UIView
        myNewView.layer.cornerRadius = boxView.layer.cornerRadius

        let activityView = UIActivityIndicatorView(style: .whiteLarge)
        activityView.center = myNewView.center

        activityView.startAnimating()

        boxView.addSubview(myNewView)
        myNewView.addSubview(activityView)
    }else{
        print("Done")
        DispatchQueue.main.async(execute: { () -> Void in
            myNewView.removeFromSuperview()
            self.view.bringSubviewToFront(boxView)
        })
        myNewView.isHidden = true
    }
}

其他选项都没有奏效,我迷失了解决方案。

编辑:我希望相同的功能在一个视图控制器中容纳三个不同的视图。

3 个答案:

答案 0 :(得分:0)

每次调用该方法时,您都在创建一个新视图,然后尝试破坏该新创建的视图。相反,应在显示视图时保存视图的引用,并在需要隐藏该实例时调用该实例的removeFromSuperview

答案 1 :(得分:0)

检查一下。

CommonMethods.swift

import UIKit

class CommonMethods: UIViewController {

static let actInd: UIActivityIndicatorView = UIActivityIndicatorView()
static let container: UIView = UIView()
static let loadingView: UIView = UIView()

static func showActivityIndicatory(uiView: UIView) {

        container.frame = uiView.frame
        container.center = uiView.center
        container.backgroundColor = UIColor(red:255/255, green:255/255, blue:255/255, alpha: 0.3)

        loadingView.frame = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 80, height: 80))
        loadingView.center = uiView.center
        loadingView.backgroundColor = UIColor(red:44/255, green:44/255, blue:44/255, alpha: 0.7)
        loadingView.clipsToBounds = true
        loadingView.layer.cornerRadius = 10

        actInd.frame = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 40, height: 40))
        actInd.style =
            UIActivityIndicatorView.Style.whiteLarge
        actInd.center = CGPoint(x: loadingView.frame.size.width / 2, y: loadingView.frame.size.height / 2);
        loadingView.addSubview(actInd)
        container.addSubview(loadingView)
        uiView.addSubview(container)
        actInd.startAnimating()
    }

static func hideActivityIndicatory(uiView: UIView) {
        container.removeFromSuperview()
        actInd.stopAnimating()
    }
}

这样的viewcontroller类调用它
CommonMethods.showActivityIndicatory(uiView: self.view)
CommonMethods.hideActivityIndicatory(uiView: self.view)

答案 2 :(得分:0)

myNewView移动到viewLoading函数范围之外,最好创建具有​​各自职责的单独方法,如下所示:

class ViewController: UIViewController {
    var loaderView: UIView?

    func showLoading(boxView: UIView, error: Bool, errorMessage: String) {
        if (self.loaderView != nil) {
            self.hideLoading()
        }

        let newView = UIView(frame: CGRect(x: 0, y: 0, width: boxView.frame.width, height: boxView.frame.height))
        newView.backgroundColor = UIColor.black.withAlphaComponent(0.75)
        newView.isOpaque = false
        // Add rounded corners to UIView
        newView.layer.cornerRadius = boxView.layer.cornerRadius

        let activityView = UIActivityIndicatorView(style: .whiteLarge)
        activityView.center = newView.center

        activityView.startAnimating()

        boxView.addSubview(newView)
        newView.addSubview(activityView)
        self.loaderView = newView
    }

    func hideLoading() {
        guard
            let loaderView = self.loaderView,
            let boxView = loaderView.superview
        else { return }

        DispatchQueue.main.async {
            loaderView.removeFromSuperview()
            self.view.bringSubviewToFront(boxView) // need this?
            self.loaderView = nil
        }
    }
}