How do I programmatically slide the UITableView down to reveal the underlying UIRefreshControl

三世轮回 提交于 2019-12-02 22:13:36

You'll have to manually change the contentOffset of your UITableView yourself. Be sure to account for the contentInset.top. It should be something as simple as:

CGPoint newOffset = CGPointMake(0, -[myTableView contentInset].top);
[myTableView setContentOffset:newOffset animated:YES];
Peter Lapisu

This will do the trick

- (void)beginRefreshingTableView {

    [self.refreshControl beginRefreshing];

    // check if contentOffset is zero
    if (fabsf(self.tableView.contentOffset.y) < FLT_EPSILON) {

        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^(void){

            self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);

        } completion:^(BOOL finished){

        }];

    }
}

For Swift 3, this is what I have based on Peter Lapisu's answer:

override func viewDidLoad() {
    super.viewDidLoad()
    self.refreshControl?.addTarget(self, action: #selector(refresh), forControlEvents: UIControlEvents.ValueChanged)
    // ...
}

func refresh(sender:AnyObject) {
    self.refreshControl?.beginRefreshing()

    if let yOffsetTable = self.tableView?.contentOffset.y {
        if yOffsetTable < CGFloat(Float.ulpOfOne) {
            UIView.animate(withDuration: 0.25, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: {
                if let refreshControlHeight = self.refreshControl?.frame.height {
                    self.tableView?.contentOffset = CGPoint(x: 0, y: -refreshControlHeight)
                }
            }, completion: nil)
        }
    }
}

For Swift 5, this is the only working version for me.

extension UIRefreshControl {

    func beginRefreshingManually() {
        if let scrollView = superview as? UIScrollView {
            scrollView.setContentOffset(CGPoint(x: 0, y: scrollView.contentOffset.y - frame.height), animated: false)
        }
        beginRefreshing()
        sendActions(for: .valueChanged)
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!