swift - 在调用方法之前添加 subview

标签 swift subview

我遇到了一个非常奇怪的问题。首先是我的代码:

func viewDuringLoading(){
        var tabBarSize = self.tabBarController?.view.bounds.size

        hider = UIView(frame: CGRectMake(0, 0, tabBarSize!.width, tabBarSize!.height))
        hider.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)


        indicator = UIActivityIndicatorView(frame: CGRectMake(hider.bounds.width/2 - 25, hider.bounds.height/2 - 25, 50, 50))

        indicator.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.6)

        hider.addSubview(indicator)
        indicator.startAnimating()

        self.tabBarController?.view.addSubview(hider)

        checkProducts()

    }

如您所见,我向 View 添加了一个 subview ,然后调用我的非异步方法 checkProducts。我现在的问题是,在我的 checkProducts 完成工作后, subview 被添加到我的 View 中。

我该如何解决这个问题?我考虑过使用异步线程。但似乎不起作用。

最佳答案

取决于您在 checkProgress 中执行的操作。根据您的说法,听起来 checkProgress 是一种同步方法,它会阻塞主线程,从而阻止您的 View 添加为 subview 。

checkProgress 方法有什么作用?当你异步运行它时发生了什么?如果它完全阻塞主线程,那么无论它是否阻塞主线程,它可能都应该是异步的。

关于swift - 在调用方法之前添加 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28268446/

相关文章:

ios - 调用 removeFromSuperview 后如何防止 subview 释放自身?

cocoa-touch - 添加一个 UINavigationController 作为 UIView 的 subview

cocoa - 创建具有可拖动文本元素的 View

ios - 子类 UIImageView layoutSubviews() 与 init()

swift - 滚动 tableview 时附件消失 | swift 5

ios - swift : How to get value from completionHandler

swift - 访问 tableView 单元格中的所有 UILabel 对象

ios - 将两个单独的文件上传到具有相同名称的Firebase

iOS viewDidLoad 未被触发

ios - 为什么当我点击它们时,我的按钮的文本颜色没有改变?