ios - 如何从父 View 中删除 uiview?

标签 ios swift uiview

<分区>

我有一个事件指示器,它在长时间运行的进程之前显示。 在此过程之后,我只是尝试再次删除该指示器。

我做不到。指示器出现但随后永远停留在我的屏幕上,即使我在主线程中将其删除也是如此。

有人可以帮助我吗?

代码:

// Activity Indicator Variables
var messageFrame = UIView()
var activityIndicator = UIActivityIndicatorView()
var strLabel = UILabel()

// in viewdidappear
showprogressIndicator("Please Wait", true)
dispatch_async(dispatch_get_main_queue()) {
    sleep(2); // LONG RUNNING TASK
    dispatch_async(dispatch_get_main_queue()) {

        for subview in self.messageFrame.subviews {
            subview.removeFromSuperview();

        }

        self.messageFrame.removeFromSuperview()
        //self.saveButton.enabled = true
    }
}



func showprogressIndicator(msg:String, _ indicator:Bool ) {
    var strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
    strLabel.text = msg
    strLabel.textColor = UIColor.whiteColor()
    var messageFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
    messageFrame.layer.cornerRadius = 15
    messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
    if indicator {
        activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
        activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        activityIndicator.startAnimating()
        messageFrame.addSubview(activityIndicator)
    }
    messageFrame.addSubview(strLabel)
    view.addSubview(messageFrame)
    //self.messageFrame.removeFromSuperview()
}

最佳答案

您的代码中有几个错误。 Sleep() 不是延迟执行某部分的函数。请看一下修订版,留作引用。


    var messageFrame: UIView!

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

        showprogressIndicator("Please Wait", true)
        dispatch_after(dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(2 * Double(NSEC_PER_SEC))
            ), dispatch_get_main_queue(), { () -> Void in
                for subview in self.messageFrame.subviews {
                    subview.removeFromSuperview();
                }
                self.messageFrame.removeFromSuperview()
        })
    }

    func showprogressIndicator(msg:String, _ indicator:Bool ) {
        let strLabel = UILabel(frame: CGRect(x: 50, y: 0, width: 200, height: 50))
        strLabel.text = msg
        strLabel.textColor = UIColor.whiteColor()
        messageFrame = UIView(frame: CGRect(x: view.frame.midX - 90, y: view.frame.midY - 25 , width: 180, height: 50))
        messageFrame.layer.cornerRadius = 15
        messageFrame.backgroundColor = UIColor(white: 0, alpha: 0.7)
        if indicator {
            let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
            activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
            activityIndicator.startAnimating()
            messageFrame.addSubview(activityIndicator)
        }
        messageFrame.addSubview(strLabel)
        view.addSubview(messageFrame)
    }

关于ios - 如何从父 View 中删除 uiview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35153610/

相关文章:

ios - 向 UIView 添加多个 subview

objective-c - Swift 和我的空闲计时器实现(缺少 CGEventType)

ios - UIScrollView 不会将点击手势传播给其子级

ios - 最佳实践是什么 : Multiple UIViews or Multiple UIViewControllers

ios - 在 iOS 中从缓冲区绘制图像

ios - CGPDFPage 绘图导致内存问题(Swift 3)

ios - 在另一个 View Controller 上显示 View Controller ,同时仍然搜索 fromViewController

iOS 8 : Why is NSLayoutAttributeCenterX failing to center my views?(适用于 iOS 7)

ios - 重置 AVPlayer 和 ARImageTracking

ios - 合并具有不同分辨率的剪辑