Search code examples
iosswiftxcodeuiscrollviewrefresh

When I pull down to refresh on my UIScrollView it requires me to pull down to the middle of the screen


I have a function: didPullToRefresh(). In order to set this function into motion, I have to pull my UIScrollView all the way to the middle of the screen. Is there a way I can refresh the data I want to refresh by only pulling the screen down 50 pixels? Thanks?

Here is the code I am currently running:

var refreshControl: UIRefreshControl!
override func viewDidLoad()  {
   super.viewDidLoad()

   scrollView.alwaysBounceVertical = true
   scrollView.bounces  = true
   refreshControl = UIRefreshControl()
   refreshControl.addTarget(self, action: #selector(didPullToRefresh), for: .valueChanged)
   self.scrollView.addSubview(refreshControl)
 }

@objc func didPullToRefresh() {

 print("Refersh")

 // For End refrshing
refreshControl?.endRefreshing()  


 }

Solution

  • Don't add target to Refresh control. Just addSubView it to scrollview. In viewDidLoad:

    scrollView.alwaysBounceVertical = true
    scrollView.bounces  = true
    scrollView.delegate = self
    
    refreshControl = UIRefreshControl()
    self.scrollView.addSubview(refreshControl)
    

    Set scrollview delegate to self. And implement following scrollview delegate method:

    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if scrollView.contentOffset.y <= -50 {
            refreshControl.beginRefreshing()
            print("Start Refresh Service")
        }
    }
    

    Write "refreshControl?.endRefreshing()" on refresh service finish.