Skip to content

Instantly share code, notes, and snippets.

@hlung
Last active October 10, 2018 09:31
Show Gist options
  • Save hlung/0ff9c310ead2ca9de3a2 to your computer and use it in GitHub Desktop.
Save hlung/0ff9c310ead2ca9de3a2 to your computer and use it in GitHub Desktop.
A UIRefreshControl beginRefreshing method that actually works
//
// UIRefreshControl+beginRefreshing.h
// Kibo
//
// Created by Hlung on 3/6/15.
// MIT License
//
#import <UIKit/UIKit.h>
@interface UIRefreshControl (beginRefreshing)
// A beginRefreshing method that actually works!
// This fixes tintColor issue and manually tableView adjust contentOffset to make sure the refresh control is visible.
- (void)beginRefreshingProgrammatically;
@end
//
// UIRefreshControl+beginRefreshing.m
// Kibo
//
// Created by Hlung on 3/6/15.
// MIT License
//
#import "UIRefreshControl+beginRefreshing.h"
@implementation UIRefreshControl (beginRefreshing)
- (void)beginRefreshingProgrammatically {
// add delay so tintColor that is set in the same runloop takes effect
[self performSelector:@selector(do_beginRefreshingProgrammatically) withObject:nil afterDelay:0];
}
- (void)do_beginRefreshingProgrammatically {
[self beginRefreshing];
// beginRefreshing doesn't make the tableView reveal the refreshControl, so we have to do it manually
UITableView *tableView = [self getParentTableViewOfView:self];
if (tableView) {
[tableView setContentOffset:CGPointMake(0, tableView.contentOffset.y-self.frame.size.height) animated:YES];
}
}
// recursively find a UITableView in superviews
- (UITableView *)getParentTableViewOfView:(UIView*)view {
if ([view.superview isKindOfClass:[UITableView class]]) {
return (UITableView *)view.superview;
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment