Refresh control target action not triggered
Asked Answered
O

3

8

My viewcontroller has a tableview, in which i populate my views in different sections. I am trying to add a refresh control to this tableview. What I have implemented is as below.

func setUpRefreshControl() {
    let refreshControl = UIRefreshControl()
    refreshControl.tintColor = UIColor.red
    refreshControl.addTarget(self, action: #selector(handleRefresh(_: )), for: UIControlEvents.valueChanged)
    if #available(iOS 10.0, *) {
        tableView.refreshControl = refreshControl
    } else {
        tableView.addSubview(refreshControl)
    }
}


@objc func handleRefresh(_ sender: UIRefreshControl) {
   print("-----------REFRESHED------------")
}

Next, when I pull down the tableview to refresh, the refresh control (red in color as setup)is visible. However, the target function is not called.

My viewcontroller is inside a tab bar controller, which is embedded in a navigation controller. I guess the issue is related to view hierarchy because when I try the same code in a separate project with no nav bar and tab bar, its working fine. But I cannot figure out what the current issue is. Any suggestion on solving this? Thank you.

EDIT: I tested it in different simulators: 6, 6s, 6s+, 7, 7+, 8, 8+, X. I found that the above code runs fine in all the plus versions including X. However, all the simulators are running ios 11.2 so I still cannot figure it out what might be causing this issue.

Ocotillo answered 9/4, 2018 at 4:39 Comment(3)
I don't think there is issue with view hierarchy here if you able to see the red refresh control. you can try to have that refresh control property global to check that – Auxin
You can uncheck under top bar and adjust automatic scrollview inset from storyboard also to try – Auxin
didn't work @PrashantTukadiya, I tried making the refresh control global and also tried changing the scroll content insets – Ocotillo
D
10

I was having a similar issue, pull to refresh would work fine on 5.5" screens but all other phones would show only a partial animation then never quite trigger the refresh.

I found invoking refreshControl.didMoveToSuperview() in the viewDidAppear for my view controller fixed the problem.

If I understand correctly, this same method is invoked when you set tableView.refreshControl = refreshControl but needs recalculation by the time the view is actually set to appear. I'll need to dig in a bit more to be sure of the specifics, but hope this is helpful for the time being. πŸ™‚

Dru answered 28/6, 2018 at 20:46 Comment(1)
This did it for me – Adrieneadrienne
A
8

I solved this issue changing the property Size in Simulated Metrics to Inferred and the property Simulated Size to Fixed in the view controller.

I hope this works for you.

Agonic answered 11/7, 2018 at 12:26 Comment(1)
To both use custom simulated size and let refresh control call the refresh method add the solution proposed by thetoneworlf : call refreshControl.didMoveToSuperview() in viewdidappear – Oil
I
1

This is the refresh control code which i implemented and its working for me in my current project, just 3 days ago.

var refreshControl: UIRefreshControl!

this is my global refreshControl variable and below is the implementation of it.

    //MARK: - REFRESH CONTROLLER VIEW
    refreshControl = UIRefreshControl()
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(refresh(_ :)), for: UIControlEvents.valueChanged)
    refreshControl.attributedTitle = NSAttributedString(string: "")
    tblRequest.addSubview(refreshControl)

Hope, this works for you ! :-)

Improve answered 9/4, 2018 at 8:19 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.