使用UIRefreshControl为一个UIWebView(Use UIRefreshContro

2019-08-02 17:44发布

只见UIRefreshControl在iOS 6中,我的问题是,如果有可能向下拉刷新的WebView,比让它在邮件弹出什么样的? 代码我用rabih是的WebView:

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

Answer 1:

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


Answer 2:

斯威夫特利用这一点,

让我们假设你想拥有拉在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];
}


Answer 3:

我刚刚加入这个很快我的代码:

[webserver.scrollView addSubview:refreshControl]

如此看来,在UIWebView有本身UIScrollView你可以只重视。 希望这对您有用。



Answer 4:

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

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

我可以在UIScrollView中和UICollectionView使用虽然。



Answer 5:

现在,我不认为它是。 实际上,你不能仅仅用任何UITableView的使用它。 该的tableView必须被正确使用的UITableViewController的一方。

这就是说,它可能您也许能坚持一个你UIWebView的上方和手动控制它的框架和价值脱身。 该的UITableViewController已经更新到了自己做的refreshControl财产的事情。



Answer 6:

看看CKRefreshControl ,你可能能够自定义您的需求。



Answer 7:

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


Answer 8:

我的答案是基于@Zaid帕坦人的答案,但更新的斯威夫特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()
}

最后,不要忘记停止重装:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    if refreshController.isRefreshing{
        refreshController.endRefreshing()
    }
}


文章来源: Use UIRefreshControl for UIWebView