Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why UIRefreshControl jumping?

I try to use the UIRefreshControl with swift 1.2 and works fine except the UI. It is jumping and I dont know why. Here is what I am doing:

class ViewController: UIViewController {

     var refreshControl:UIRefreshControl!

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.refreshControl = UIRefreshControl()
        self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
        self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
        self.tableView.addSubview(refreshControl)
    }

    func refresh(sender:AnyObject) {
        // Code to refresh table view
    }

}

And you can download my poc project from here: POC project

While you pull down the tableView, suddenly jump a little bit down. Has anybody any idea why?

If I use with UITableViewController, It works fine but I dont want to use this UI object. I would like to use a simple UITableView in a UIViewController.

like image 908
szuniverse Avatar asked Jun 23 '15 19:06

szuniverse


2 Answers

I have experienced this problem and I believe it is because the UIRefreshControl was meant for UITableViewController.

I think you should try to insert the view at index 0 like below. (Excuse the objective-c)

[self.tableView insertSubview:_refreshControl atIndex:0];

Edit

Do you have code to stop the refreshControl?

    // End Refreshing
    if ([self.refreshControl isRefreshing]) {
        float delayInSeconds = 1.0;

        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^{
            [self.refreshControl endRefreshing];

        });
    }
like image 170
egarlock Avatar answered Nov 10 '22 01:11

egarlock


I found a solution, it works:

override func viewDidLoad() {
    super.viewDidLoad()

    self.refreshControl = UIRefreshControl()
    self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)


    var tableViewC = UITableViewController()
    tableViewC.refreshControl = self.refreshControl

    self.tableView = tableViewC.tableView

    self.view.addSubview(self.tableView)
}
like image 25
szuniverse Avatar answered Nov 09 '22 23:11

szuniverse