I have about 5 UIScrollView
's already in my app which all load multiple .xib
files. We now want to use a UIRefreshControl
. They are built to be used with UITableViewControllers (per UIRefreshControl class reference). I do not want to re-do how all 5 UIScrollView
work. I have already tried to use the UIRefreshControl
in my UIScrollView
's, and it works as expected except for a few things.
Just after the refresh image turns into the loader, the
UIScrollView
jumps down about 10 pixels, which only does not happen when I am very careful to drag theUIScrollview
down very slowly.When I scroll down and initiate the reload, then let go of the
UIScrollView
, theUIScrollView
stays where I let it go. After it is finished reloading, theUIScrollView
jumps up to the top with no animation.
Here is my code:
-(void)viewDidLoad
{
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[myScrollView addSubview:refreshControl];
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
[refresh endRefreshing];
}
Is there any way I can save a bunch of time and use a UIRefreshControl
in a UIScrollView
?
Thank You!!!
Here is how you do this in C# / Monotouch. I cant find any samples for C# anywhere, so here it is.. Thanks Log139!
I got a
UIRefreshControl
to work with aUIScrollView
:Need to do the data update on a separate thread or it will lock up the main thread (which the UI uses to update the UI). So while the main thread is busy updating the data, the UI is also locked up or frozen and you never see the smooth animations or spinner.
EDIT: ok, I'm doing the same thing as OP and i've now added some text to it (ie, "Pull to Refresh") and it does need to get back onto the main thread to update that text.
Updated answer.
You can simply create an instance of the refresh control and add it at the top of the scroll view. then, in the delegate methods you adjust its behavior to your requirements.
How to do it in Swift 3:
For the Jumping issue, Tim Norman's answer solves it.
Here is the swift version if you are using swift2:
I made
UIRefreshControl
work properly insideUIScrollView
. I inheritedUIScrollView
, blocked changing of contentInset and overrided contentOffset setter: