While pulling down to refresh, the controller target method is not being called.
Why is this issue happening?
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. This refreshControl will appear when you create a UIRefereshControl and assign it to this property. No need to add UIRefereshControl as a subview for scroll.
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()
}
}
Related
I have a scrollable content in my view. I want to add pull to refresh in it. I try to add but action is not triggered.
I tried many couple of ways & re-search also but none of it worked for me. I have iOS 10 as minimum deployment target.
Try1:
let refreshControl = UIRefreshControl()
self.scl_view.alwaysBounceVertical = true
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scl_view.addSubview(refreshControl)
Try2:
let refreshControl = UIRefreshControl()
self.scl_view.alwaysBounceVertical = true
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scl_view.refreshControl = refreshControl
//MARK:- Refresh control
#objc func pullToRefresh(_ refreshControl: UIRefreshControl) {
// Update your conntent here
self.setupData()
//refreshControl.endRefreshing()
}
select your tablviewcontroller
goto inspector
find refreshing select enabled
goto document outline and select refresh control
control drag or right click(hold) drag to your controller file and call function on it to refresh data
it is not available on tableview
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
// Add Refresh Control to collection View
if #available(iOS 10.0, *) {
collectionView.refreshControl = refreshControl
} else {
collectionView.addSubview(refreshControl)
}
refreshControl.tintColor = UIColor(red:0.25, green:0.72, blue:0.85, alpha:1.0)
// Configure Refresh Control
refreshControl.addTarget(self, action: #selector(fetchData(_:)), for: .valueChanged)
}
#objc private func fetchData(_ sender: Any) {
DispatchQueue.main.async {
//Fuction which you want to call
self.refreshControl.endRefreshing()
}
}
Hi in my application I want to add Pull to refresh controller to scrollview.
I write the following code but it don't getting any response.
When ever I scroll the view on that time add target method is not called.
#IBOutlet var scrool: UIScrollView!
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
// Add refresh control for the Home page scroll view.
self.refreshControl.tintColor = UIColor.appcolor
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: UIControl.Event.valueChanged)
self.scrool.isScrollEnabled = true
self.scrool.alwaysBounceVertical = true
scrool.addSubview(refreshControl)
}
#objc func refresh(sender:AnyObject) {
// Code to refresh table view
self.getHomePageApi()
refreshControl.endRefreshing()
}
Hi I resolved this issue when I write this code in viewwillappear() except viewdidload() function it will working fine.
var refreshControl:RefreshControl!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
refreshControl = RefreshControl()
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
scrool.refreshControl = refreshControl
}
#objc func refresh()
{
// Code to refresh table view
refreshControl.endRefreshing()
self.getHomePageApi()
}
I have an ActivityIndicator on my tableView which appears when I pull to refresh.
I was wondering if I can change the color of the spinning indicator, here is my implimentation:
override func viewDidLoad() {
refreshData()
super.viewDidLoad()
self.refreshControl?.addTarget(self, action: #selector(handleRefresh(refreshControl:)), for: UIControlEvents.valueChanged)
}
func handleRefresh(refreshControl: UIRefreshControl) {
self.tableView.reloadData()
self.refreshData()
sleep(2)
refreshControl.endRefreshing()
}
Simply change the tintColor of refreshControl
self.refreshControl.tintColor = .red
I've implemented a straight forward WKWebView in iOS.
var refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: Selector("refreshWebView"), forControlEvents: UIControlEvents.ValueChanged)
This is in viewDidLoad()
And the corresponding refreshWebView function. But when I run it on mobile or simulator, no matter how much I drag, there is no pull to refresh action. Nothing is triggered, and it almost feels like I am stuck to the upper bounds of the screen. Where can be going wrong?
Try to change your code like this, If you want to pull to refresh the webView you need to addSubView UIRefreshControl object in the ScrollView of webView like this
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(self.refreshWebView(_:)), forControlEvents: UIControlEvents.ValueChanged)
webView.scrollView.addSubview(refreshControl)
Add func like this
func refreshWebView(sender: UIRefreshControl) {
print("refersh")
webView.loadRequest(NSURLRequest(URL: NSURL(string: "https://google.com")!))
sender.endRefreshing()
}
Hope this will help you.
SWIFT 4 Solution (Based on Nirav D answer)
You can simply call this setup method in your viewDidLoad:
private func setupRefreshControl() {
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(refreshWebView(sender:)), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)
}
#objc
private func refreshWebView(sender: UIRefreshControl) {
print("refreshing...")
webView.load(URLRequest(url: url))
sender.endRefreshing()
}
Just to say looking at this in Swift 3 and the answer from JAck helped me solve this issue:
in loadView()/viewDidLoad()
webView.scrollView.bounces = true
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(ViewController.refreshWebView), for: UIControlEvents.valueChanged)
webView.scrollView.addSubview(refreshControl)
and then a separate function
func refreshWebView(sender: UIRefreshControl) {
print("refersh")
//
sender.endRefreshing()
}
And I'm now seeing the refresh being printed.
You have to enable bouncing for the scrollview of the webview:
webView.scrollView.bounces = true
This will let you drag the webview down which will trigger your UIRefreshControl.
Following code will put the refresh control in scrollview of webview.Initially it will load google.com. To see pull to refresh clearly I have set white color to the background of scroll view so it is clearly visible and on pull to refresh webview opens facebook page.
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.refreshControl = UIRefreshControl.init()
refreshControl!.addTarget(self, action:#selector(refreshControlClicked), for: UIControlEvents.valueChanged)
self.webView.scrollView.addSubview(self.refreshControl!)
self.webView.scrollView.backgroundColor = UIColor.white
self.webView.delegate = self
let url:URL = URL.init(string:"https://www.google.com")!
self.loadRequestWithUrl(URLRequest.init(url: url))
}
func webViewDidStartLoad(_ webView: UIWebView) {
NSLog("website loaded")
}
func loadRequestWithUrl(_ urlRequest : URLRequest?){
if(urlRequest != nil){
self.webView.loadRequest(urlRequest!)
}
}
func refreshControlClicked(){
let url:URL = URL.init(string:"https://www.facebook.com")!
self.loadRequestWithUrl(URLRequest.init(url: url))
}
Try to declare this like below.
refreshControl.addTarget(self, action: #selector(yourViewController.refreshWebView), forControlEvents: UIControlEvents.ValueChanged)
Hope this works..
I used the following code at a UICollectionViewController
override func viewDidLoad() {
self.collectionView!.alwaysBounceVertical = true
let refresher = UIRefreshControl()
refresher.addTarget(self, action: "refreshStream", forControlEvents: .ValueChanged)
refreshControl = refresher
collectionView!.addSubview(refreshControl!)
}
func refreshStream() {
print("refresh")
self.collectionView?.reloadData()
refreshControl?.endRefreshing()
}
Now I need it to work with a UICollectionView inside a UIViewController and I googled for an hour now but can't get it working.
I appreciate any help.
New swift code changed in calling action method you could do rewrite like this
#IBOutlet weak var collectionView: UICollectionView!
var refresher:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.refresher = UIRefreshControl()
self.collectionView!.alwaysBounceVertical = true
self.refresher.tintColor = UIColor.red
self.refresher.addTarget(self, action: #selector(loadData), for: .valueChanged)
self.collectionView!.addSubview(refresher)
}
#objc func loadData() {
self.collectionView!.refreshControl.beginRefreshing()
//code to execute during refresher
.
.
.
stopRefresher() //Call this to stop refresher
}
func stopRefresher() {
self.collectionView!.refreshControl.endRefreshing()
}
Swift 5 solution
As #fishspy already mentioned, that's the way to put your collection view inside a view controller, but I'm gonna share also how to connect your refresh control to the collection view in a cleaner way.
Since iOS 10 there's a dedicated property for the refresh control. Apart of that, I'd also recommend to directly initialise your refresh control as a property, declaring it private and doing the following things:
#IBOutlet private weak var collectionView: UICollectionView!
private let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(didPullToRefresh(_:)), for: .valueChanged)
collectionView.alwaysBounceVertical = true
collectionView.refreshControl = refreshControl // iOS 10+
}
#objc
private func didPullToRefresh(_ sender: Any) {
// Do you your api calls in here, and then asynchronously remember to stop the
// refreshing when you've got a result (either positive or negative)
refreshControl.endRefreshing()
}
var refreshControl:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: #selector(PricingPlansCollectionViewController.reload), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
func refresh(sender:AnyObject)
{
//DO
}
From the storyboard, you need to link the collection view to the appropriate controller file, using Ctrl + Drag from the collection view object. It needs to be linked via an #IBOutlet.
Also, you should Ctrl + Drag from the collection view to the view controller object on the storyboard and select Data Source and Delegate so that the collection view is correctly linked.
Let me know if this helps or if I have misunderstood your situation.
For me, i had:
func setupCollectionView(){
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
self.refreshControl.addTarget(self, action: #selector(self.reload), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
It still wasn't working (I believe i had constraints preventing it somehow), so i added:
collectionView.alwaysBounceVertical = true
then all was good. Just in case someone else is facing the same issue.
For a better understanding, the full implementation
#IBOutlet private weak var collectionView: UICollectionView!
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
func setupCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
self.refreshControl.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
#objc func refresh(_ sender:AnyObject) {
//do refreshing stuff
}
private let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(youFunction), for: .valueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "Fetching Data ...", attributes: nil)
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
yourCollectionViewOrTableView.reloadData()
}