swift 下拉刷新

swift pull to refresh

我有我的 refreshcontrollerscrollview..

  self.refreshControl = UIRefreshControl()
        self.refreshControl.attributedTitle = NSAttributedString(string: "Frissítéshez húzzad! :)")
        self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
        self.scrollView.addSubview(refreshControl)

func refresh(sender:AnyObject)
    {

//my refresh code here..

            self.refreshControl.endRefreshing()
    }

我将 subview 添加到 scrollView。当页面内容是 overflowing 屏幕时,它正在工作。但是当我没有收到太多数据并且它不是 overflowing 时,问题就出现了,pull and refresh 功能不起作用。 :(

我没有使用 tableview

谁能帮我解决这个问题?

self.scrollView.scrollEnabled = true
self.scrollView.alwaysBounceVertical = true

var alwaysBounceVertical: Bool // default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically

此 UIRefreshControl 不适用于 UIScrollView。它应该通过关联的 table 视图控制器对象与 table 链接。

Note: Because the refresh control is specifically designed for use in a table view that's managed by a table view controller, using it in a different context can result in undefined behavior.

UIRefreshControl Class Reference

iOS 10 次更新

alwaysBounceVertical不完全正确,refreshControl已经介绍:

  1. 所有 iOS 版本:如果 contentSize 小于 frame
  2. ,则始终需要 bounces
  3. 前iOS10个版本: alwaysBounceVertical小内容也需要
  4. iOS 10+: 使用 refreshControl[=71= 通知 UIScrollView UIRefreshControl 的存在]

- iOS 10

UIRefreshControl 现在由 UIScrollView 支持,使用 refreshControl 就像之前的 OS.
中的 UITableView 这意味着拖放体验是完美无缺的。
点击并拖动下面动画中的白色箭头:它们保持同步

↻ replay animation

- iOS 9 及更早版本

您可以将 UIRefreshControl 手动添加到 UIScrollView,但该视图无法识别此类元素,并且刷新的拉动往往会发生漂移。
请注意在下面的动画中拉动刷新有多困难:白色箭头的滚动会漂移,需要更远的距离才能触发控件

↻ replay animation


绕过 refreshControl OS 与此滚动视图的差异 extension:

var _refreshControl : UIRefreshControl? {
    get {
        if #available(iOS 10.0, *) {
            return refreshControl
        } else {
            return subviews.first(where: { (view: UIView) -> Bool in
                view is UIRefreshControl
            }) as? UIRefreshControl
        }
    }

    set {
        if #available(iOS 10.0, *) {
            refreshControl = newValue
        } else {
            // Unique instance of UIRefreshControl added to subviews
            if let oldValue = _refreshControl {
                oldValue.removeFromSuperview()
            }
            if let newValue = newValue {
                insertSubview(newValue, at: 0)
            }
        }
    }
}

► 在 GitHub 上找到此解决方案。

是的我们可以对UIScrollView、UITableview、UicollectionVIew使用刷新控件..

这是 PULL TO REFRESH,

的代码
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()  


 }