为什么下拉刷新不适用于滚动视图

问题描述

下拉刷新时,未调用控制器目标方法

为什么会出现这个问题?

  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()


    }

解决方法

iOS 10 > UIScrollView has a refreshControl property。当您创建 UIRefereshControl 并将其分配给此属性时,将出现此 refreshControl。 无需将 UIRefereshControl 添加为滚动子视图。

func configureRefreshControl () {
   // Add the refresh control to your UIScrollView object.
   myScrollingView.refreshControl = UIRefreshControl()
   myScrollingView.refreshControl?.addTarget(self,action:
                                      #selector(handleRefreshControl),for: .valueChanged)
}
    
@objc func handleRefreshControl() {
   // Update your content…

   // Dismiss the refresh control.
   DispatchQueue.main.async {
      self.myScrollingView.refreshControl?.endRefreshing()
   }
}

enter image description here