为UIWebView使用UIRefreshControl

我看到了iOS 6中的UIRefreshControl,我的问题是如果可以通过拉下WebView来刷新WebView,并让它像邮件一样popup来呢? 我使用rabih的代码是WebView:

UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged]; [rabih addSubview:rabih]; 

这是你如何使用下拉来刷新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]; } 

Swift中使用这个,

假设你想要在WebView中刷新,

所以试试这个代码:

 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() } 

在Objective-C中,我使用了这个:

 - (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]; } 

我刚刚添加了这个很快我的代码:

 [webserver.scrollView addSubview:refreshControl] 

所以看起来UIWebView本身就有一个你可以附加的UIScrollView 。 希望它对你有用。

我其实已经尝试过,并得到以下错误。

*终止应用程序由于未捕获的exception“NSInternalInconsistencyException”,原因:“UIRefreshControl只能由UITableViewControllerpipe理”

我可以在UIScrollView和UICollectionView中使用。

现在我不相信。 你实际上不能只用任何UITableView来使用它。 tableView需要成为UITableViewController的派对才能正确使用。

也就是说,可能你可能会在UIWebView的上面粘贴一个,并手动控制它的框架和值。 事情的UITableViewController已被更新,以自己的refreshControl属性。

看一下CKRefreshControl ,你可以根据自己的需要进行定制。

扩展@ umut-can-köseali答案在刷新结束时对UIRefreshControl有更好的控制:

 - (void)viewDidLoad { [super viewDidLoad]; _refreshControl = [[UIRefreshControl alloc] init]; [_refreshControl addTarget:self action:@selector(handleWebViewRefresh:) forControlEvents:UIControlEventValueChanged]; [self.webView.scrollView addSubview:_refreshControl]; //<- this is point to use. Add "scrollView" property. } - (void)webView:(UIWebView *)_webView didFailLoadWithError:(NSError *)error { [_refreshControl endRefreshing]; } - (void)webViewDidFinishLoad:(UIWebView *)_webView { NSString *navigatorUrl = _webView.request.URL.absoluteString; if( navigatorUrl && ![navigatorUrl isEqualToString:@""]) { NSString *host=_webView.request.URL.host; NSString *path=_webView.request.URL.path; [_refreshControl endRefreshing]; } }