Use UIRefreshControl for UIWebView

2020-05-19 04:46发布

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

8条回答
forever°为你锁心
2楼-- · 2020-05-19 04:57

I've actually tried that and got error following.

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UIRefreshControl may only be managed by a UITableViewController'

I could use in UIScrollView and UICollectionView though.

查看更多
再贱就再见
3楼-- · 2020-05-19 05:01

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];
}
查看更多
迷人小祖宗
4楼-- · 2020-05-19 05:03

Have a look at CKRefreshControl, which you may be able to customize to your needs.

查看更多
Melony?
5楼-- · 2020-05-19 05:07

Right now I don't believe it is. You actually can't use it with just any UITableView. The tableView needs to be party of a UITableViewController to be used properly.

That said, its possible you might be able to get away with sticking one above your UIWebView and controlling its frame and values manually. Things that the UITableViewController has been updated to do on its own with its refreshControl property.

查看更多
仙女界的扛把子
6楼-- · 2020-05-19 05:08

My answer is based on @Zaid Pathan answer but updated for Swift 4.

class WebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

var refreshController:UIRefreshControl!
var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
//Your webView initializing

    webView.scrollView.bounces = true //DONT FORGET IT!!!

    refreshController = UIRefreshControl()

    refreshController.bounds = CGRect(x: 0, y: 50, width: refreshController.bounds.size.width, height: refreshController.bounds.size.height)
    refreshController.addTarget(self, action: #selector(self.refreshWebView(refresh:)) , for: UIControlEvents.valueChanged)
    refreshController.tintColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    webView.scrollView.addSubview(refreshController)
}

@objc func refreshWebView(refresh:UIRefreshControl){
    webView.reload()
}

Finally, don't forget to stop reloading:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    if refreshController.isRefreshing{
        refreshController.endRefreshing()
    }
}
查看更多
何必那么认真
7楼-- · 2020-05-19 05:09

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.

查看更多
登录 后发表回答