在调用方法之前添加子视图

Add subview before calling method

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

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()

    }

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

我该如何解决这个问题?我考虑过使用异步线程。但是好像不行。

取决于您在 checkProgress 中所做的事情。根据你所说的,听起来 checkProgress 是一种阻塞主线程的同步方法,它会阻止你的视图被添加为子视图。

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