UIRefreshControl bug when entering foreground

前端 未结 3 1752
小蘑菇
小蘑菇 2021-02-19 16:49

I\'ve noticed a little bug (but really annoying) when I use UIRefreshControl in my View Controller. When the application returns from the background the UIRe

3条回答
  •  甜味超标
    2021-02-19 17:25

    This bug seems to occur when making a modal presentation over the table view controller as well as when the app returns from the background.

    The simplest fix is to call endRefreshing() in viewWillAppear(_:) and upon receiving the notification UIApplicationWillEnterForeground. You need to do both because viewWillAppear(_:) is not called when the app returns from the background.

    The effect of calling endRefreshing() on an instance of UIRefreshControl appears to be to return the control to the correct location in the view hierarchy and ensure that it continues to animate correctly on subsequent refreshes.

    Remember to check that your refresh control is, in fact, not refreshing.

    In Swift:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        endRefreshing()
    
        NotificationCenter.default.addObserver(self,
            selector: #selector(endRefreshing),
            name: Notification.Name.UIApplicationWillEnterForeground,
            object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        NotificationCenter.default.removeObserver(self,
            name: Notification.Name.UIApplicationWillEnterForeground,
            object: nil)
    }
    
    func endRefreshing(_ notification: Notification? = nil) {
        if refreshControl?.isRefreshing == false {
            refreshControl?.endRefreshing()
        }
    }
    

    Tested in Xcode 7.0 targeting iOS 8.0 with a UITableViewController configured in the storyboard.

提交回复
热议问题