xcode 为 UIWebView 使用 UIRefreshControl

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13379815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:05:31  来源:igfitidea点击:

Use UIRefreshControl for UIWebView

iosxcodewebviewios6

提问by David G?lzh?user

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:

我在 iOS 6 中看到了 UIRefreshControl,我的问题是是否可以通过下拉刷新 WebView 而不是让它像在邮件中一样弹出?我使用 rabih 的代码是 WebView:

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

回答by Umut Koseali

This is how you can use pull down to refresh on UIWebview:

这是您可以使用下拉刷新 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];
}

回答by Zaid Pathan

In Swiftuse this,

Swift 中使用这个,

Let's assume you wants to have pull to refresh in WebView,

假设您想在 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:

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

回答by rubenfonseca

I've just added this very quickly to my code:

我刚刚在我的代码中快速添加了这个:

[webserver.scrollView addSubview:refreshControl]

So it seems that the UIWebViewhas itself a UIScrollViewyou can just attach to. Hope it's useful for you.

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

回答by Tomohisa Takaoka

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'

*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UIRefreshControl 可能只能由 UITableViewController 管理”

I could use in UIScrollView and UICollectionView though.

不过我可以在 UIScrollView 和 UICollectionView 中使用。

回答by Ryan Poolos

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.

现在我不相信它是。您实际上不能将它与任何 UITableView 一起使用。tableView 需要是 UITableViewController 的一部分才能正确使用。

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.

也就是说,您可能可以在 UIWebView 上方粘贴一个并手动控制其框架和值。UITableViewController 已更新为使用其 refreshControl 属性自行完成的事情。

回答by runmad

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

查看CKRefreshControl,您可以根据需要对其进行自定义。

回答by loretoparisi

Extending @umut-can-k?seali answer to have better control of the UIRefreshControl when refresh ends:

扩展 @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];
    }
}

回答by Had?i Lazar Pe?i?

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

我的回答基于@Zaid Pathan 的回答,但针对 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()
    }
}