How do I “hide” a UIRefreshControl?

前端 未结 12 965
醉梦人生
醉梦人生 2020-12-15 16:16

Occasionally my table view won\'t be connected to a service to refresh, and in that case, I don\'t want the UIRefreshControl to be present.

After I add it in viewDid

12条回答
  •  旧巷少年郎
    2020-12-15 17:16

    An old question, but I was looking for an answer and nothing worked exactly like I wanted.

    This is what worked for me:

    Swift 4

    func createRefreshControl() {
        refreshControl = UIRefreshControl()
        refreshControl?.addTarget(self, action: #selector(self.myTableRefreshFunction), for: UIControlEvents.valueChanged)
        refreshControl?.tintColor = UIColor.white
        refreshControl?.endRefreshing()
    }
    
    func removeRefreshControl() {
        refreshControl?.removeTarget(self, action: #selector(self.myTableRefreshFunction), for: UIControlEvents.valueChanged)
        refreshControl = nil
    }
    

    I call createRefreshControl() when I want the control created, and removeRefreshControl when I want it removed.

    I had to remove the same target I initially added to the refresh control, otherwise it would refresh one time before it was actually removed.

提交回复
热议问题