Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created April 22, 2017 14:02
Show Gist options
  • Save odrobnik/ae16f4f071ead51d915712818a2279d8 to your computer and use it in GitHub Desktop.
Save odrobnik/ae16f4f071ead51d915712818a2279d8 to your computer and use it in GitHub Desktop.
@objc protocol Refreshable
{
/// The refresh control
var refreshControl: UIRefreshControl? { get set }
/// The table view
var tableView: UITableView! { get set }
/// the function to call when the user pulls down to refresh
@objc func handleRefresh(_ sender: Any);
}
extension Refreshable where Self: UIViewController
{
/// Install the refresh control on the table view
func installRefreshControl()
{
let refreshControl = UIRefreshControl()
refreshControl.tintColor = .primaryColor
refreshControl.addTarget(self, action: #selector(handleRefresh(_:)), for: .valueChanged)
self.refreshControl = refreshControl
if #available(iOS 10.0, *)
{
tableView.refreshControl = refreshControl
}
else
{
tableView.backgroundView = refreshControl
}
}
}
@aalenliang
Copy link

My code is very likely to these, and now I have a ViewController, which uses UICollectionView. Since any kind of scroll view has the property refreshControl since iOS 10, I want to extend my protocol so that the view can be UIScrollView or its subclass. But I can't figure out how to achieve that.

@jeremiasdsa
Copy link

@aalenliang, u just need to change the tableView property to a scrollView property on your protocol and add scrollView.refreshControl = refreshControl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment