I saw the UIRefreshControl in iOS 6 and my question is if it is possible to refresh a WebView by pulling it down and than let it pop up like in mail? Code I used rabih is the WebView:
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[rabih addSubview:rabih];
This is how you can use pull down to refresh on UIWebview:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Make webView a delegate to itself
// I am going to add URL information
NSString *fullURL = @"http://www.umutcankoseali.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
_webView.delegate = (id)self;
[_webView loadRequest:requestObj];
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
[_webView.scrollView addSubview:refreshControl]; //<- this is point to use. Add "scrollView" property.
}
-(void)handleRefresh:(UIRefreshControl *)refresh {
// Reload my data
NSString *fullURL = @"http://www.umutcankoseali.com/";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
[refresh endRefreshing];
}
In Swift use this,
Let's assume you wants to have pull to refresh in WebView,
So try this code:
override func viewDidLoad() {
super.viewDidLoad()
addPullToRefreshToWebView()
}
func addPullToRefreshToWebView(){
var refreshController:UIRefreshControl = UIRefreshControl()
refreshController.bounds = CGRectMake(0, 50, refreshController.bounds.size.width, refreshController.bounds.size.height) // Change position of refresh view
refreshController.addTarget(self, action: Selector("refreshWebView:"), forControlEvents: UIControlEvents.ValueChanged)
refreshController.attributedTitle = NSAttributedString(string: "Pull down to refresh...")
YourWebView.scrollView.addSubview(refreshController)
}
func refreshWebView(refresh:UIRefreshControl){
YourWebView.reload()
refresh.endRefreshing()
}
In Objective-C, I used this:
- (void)addPullToRefreshToWebView{
UIColor *whiteColor = [UIColor whiteColor];
UIRefreshControl *refreshController = [UIRefreshControl new];
NSString *string = @"Pull down to refresh...";
NSDictionary *attributes = @{ NSForegroundColorAttributeName : whiteColor };
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string attributes:attributes];
refreshController.bounds = CGRectMake(0, 0, refreshController.bounds.size.width, refreshController.bounds.size.height);
refreshController.attributedTitle = attributedString;
[refreshController addTarget:self action:@selector(refreshWebView:) forControlEvents:UIControlEventValueChanged];
[refreshController setTintColor:whiteColor];
[self.webView.scrollView addSubview:refreshController];
}
- (void)refreshWebView:(UIRefreshControl*)refreshController{
[self.webView reload];
[refreshController endRefreshing];
}
I've just added this very quickly to my code:
[webserver.scrollView addSubview:refreshControl]
So it seems that the UIWebView
has itself a UIScrollView
you can just attach to. Hope it's useful for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With