Swift - UIRefresh

UIRefresh

Posted by 温宇直 on June 13, 2016

UIRefreshControl通常用于UITableViewUICollectionView的下拉刷新。一个UIRefreshControl控件通常可以修改其tintColorattributedTitle的值。

一开始UIRefreshControl初始化完成后,想要添加到tableView的时候,总想着tableView应该有个setRefreshControl()方法可以用来设置这个控件。最后查阅资料的时候才发现,要将刷新控件添加到tableView的话,只需要调用addSubview方法,然后添加Value Changed的Target-Action即可

...
var refreshControl: UIRefresh?

...

refreshControl = UIRefreshControl()
refreshControl!.tintColor = UIColor.whiteColor()
refreshControl!.attributedTitle = NSAttributedString(string: String(NSDate()), attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])
refreshControl!.addTarget(self, action: #selector(ViewController.refresh), forControlEvents: .ValueChanged)
tableView.addSubview(refreshControl!)

如果是自定义的UIRefreshControl的话,可以利用xib或者用代码的文件,然后将该view作为UIRefreshControl的子视图。可以参考iOS Custom Pull-to-Refresh Control Tutorial (Updated for Swift)