14

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

8 回答 8

49

这是您可以使用下拉刷新 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];
}
于 2013-05-02T20:36:28.617 回答
9

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];
}
于 2015-05-07T14:11:14.257 回答
5

我刚刚很快将其添加到我的代码中:

[webserver.scrollView addSubview:refreshControl]

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

于 2013-04-07T17:46:09.240 回答
3

我实际上已经尝试过了,但出现了错误。

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

我可以在 UIScrollView 和 UICollectionView 中使用。

于 2012-11-14T21:32:47.740 回答
0

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

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

于 2012-11-14T13:38:01.890 回答
0

看看CKRefreshControl,您可以根据自己的需要进行自定义。

于 2012-11-14T13:52:08.493 回答
0

我的答案基于@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()
}

最后,不要忘记停止重新加载:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    if refreshController.isRefreshing{
        refreshController.endRefreshing()
    }
}
于 2018-01-11T00:02:50.897 回答
0

扩展 @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];
    }
}
于 2015-08-31T14:30:50.303 回答