How to use pull to refresh in Swift?
Pull to refresh is built in iOS. You could do this in swift like
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(self.refresh(_:)), for: .valueChanged)
tableView.addSubview(refreshControl) // not required when using UITableViewController
}
@objc func refresh(_ sender: AnyObject) {
// Code to refresh table view
}
At some point you could end refreshing.
refreshControl.endRefreshing()
Pull to refresh default refresh level change
Since I do not think you can change the behaviour of how much distance you have to travel to initiate the refresh I would just trigger it manually when user scrolled enough, something like:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
guard !refreshControl.isRefreshing else {
return//do nothing if we are already refreshing
}
//set your threshold to whatever feels ok (I used -30 here)
if scrollView.contentOffset.y < -30 {
refreshTable()
refreshControl.beginRefreshing()
}
}
You also might have to play a bit with offsetting table view properly when refresh is active so that the UIActivityIndicator
is above your cells, and then adjust it again when you finish refreshing. Note you will have to call refreshControl.endRefreshing()
in refreshTable()
method once API
calls are completed or whatever you are doing there...
Change Pull To Refresh Activity Indicator
Simply change the tintColor
of refreshControl
self.refreshControl.tintColor = .red
How can change the color of text of Pull to refresh on UITableView?
Try:
//the color of the background
self.refreshControl.backgroundColor = [UIColor purpleColor];
//the color of the spinner
self.refreshControl.tintColor = [UIColor whiteColor];
// the color of the label
NSString *title = @"loading...";
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor]
forKey:NSForegroundColorAttributeName];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attrsDictionary];
self.refreshControl.attributedTitle = attributedTitle;
Fix iOS pull to refresh animation
Adding refresh control as a subview could be a problem. UITableView now have property for the refresh control. Here you have description from apple documentation how you should implement that:
https://developer.apple.com/documentation/uikit/uirefreshcontrol
iOS swift pull to refresh mixes with tableview
You can implement refresh control like this.
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet var tableView: UITableView!
var refreshControl : UIRefreshControl!
}
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl.backgroundColor = UIColor.clearColor()
self.refreshControl.tintColor = UIColor.blackColor()
self.refreshControl.addTarget(self, action: "methodPullToRefresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(self.refreshControl)
}
func methodPullToRefresh(sender:AnyObject)
{
self.refreshControl?.beginRefreshing()
}
// Once you are done with your task
self.refreshControl?.endRefreshing()
// Main queue thread is only required when refresh controls comes or goes off with delay, if it works quickly then no need to add this
dispatch_async(dispatch_get_main_queue()) {
}
Hope, this will resolve your problem.
All the best.
Change pull to refresh text color with swift
Try
var attr = [NSForegroundColorAttributeName:UIColor.greenColor()]
refresher.attributedTitle = NSAttributedString(string: "hey ! is there something new ?", attributes:attr)
Latest Swift 5.6
var attr = [NSAttributedString.Key.foregroundColor: UIColor.white]
refresher.attributedTitle = NSAttributedString(string: "hey ! is there something new ?", attributes:attr)
How Do I Shorten the Pull Distance on UIRefreshControl to Activate the Pull to Refresh Action?
you can still use refreshControl but with some modifications!
add these code to your viewController:
var canRefresh = true
override func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y < -100 { //change 100 to whatever you want
if canRefresh && !self.refreshControl.refreshing {
self.canRefresh = false
self.refreshControl.beginRefreshing()
self.refresh() // your viewController refresh function
}
}else if scrollView.contentOffset.y >= 0 {
self.canRefresh = true
}
}
and as usual in the end of your refresh logic in self.refresh() function add :
self.refreshControl.endRefreshing()
Related Topics
Swift Array Append Overwriting Other Array Values
Sizing a Container View with a Controller of Dynamic Size Inside a Scrollview
iOS App, Programmatically Get Build Version
Module Was Not Compiled for Testing' When Using @Testable
Afnetworking 2.0 Track File Upload Progress
Fbsession: an Attempt Was Made Reauthorize Permissions on an Unopened Session
Ios: Present View Controller Programmatically
Transparent Uinavigationbar in Swift
Present a View Controller, Dismiss It and Present a Different One in Swift
Modify Pull to Refresh in Swift
Iphone: Can a Dev Other Than Team Agent Build an App for Distribution
How to Dismiss Keyboard When Touching Anywhere Outside Uitextfield (In Swift)
iOS 8.1 Simulator Localization Broken (Nslocalizedstring)
Cagradientlayer Diagonal Gradient
Create a Button Programmatically and Set a Background Image
How to Track User Location in Background
Ios: Can't Save File to 'Application Support' Folder, But Can to 'Documents'