【问题标题】:Use UIRefreshControl for UIWebView为 UIWebView 使用 UIRefreshControl
【发布时间】:2012-11-03 00:11:04
【问题描述】:

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

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

【问题讨论】:

  • 你确定你需要UIWebView而不是UITableView吗?
  • @woz 是的,我想用这种方法刷新一个名为 rabih 的 UIWebView。
  • 我不确定你想要的效果,但你不能使用 JavaScript 吗?
  • @woz 这适用于 iOS 而非 OSX
  • 只是评论:使用“拉动刷新”手势重新加载 UIWebView 并不是一个很好的用户体验。这些通常对垂直滚动视图很有用,尤其是 UITableView

标签: ios xcode webview ios6


【解决方案1】:

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

也就是说,您可以通过在 UIWebView 上方粘贴一个并手动控制其框架和值来摆脱困境。 UITableViewController 已更新为使用其 refreshControl 属性自行执行的操作。

【讨论】:

  • 我知道这是可能的,因为我知道有一款应用可以做到这一点。
  • 即使有可能并不意味着你应该这样做。他们可能没有使用 UIRefreshControl 并且 simple 擅长复制 Apple 的风格,就像过去很多次一样。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

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

    我可以在 UIScrollView 和 UICollectionView 中使用。

    【讨论】:

      【解决方案4】:

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

      [webserver.scrollView addSubview:refreshControl]
      

      因此,UIWebView 本身似乎有一个 UIScrollView,您可以附加到该UIScrollView。希望对你有用。

      【讨论】:

      • 您能否编辑您的答案以完成问题。您是如何更改代码的?
      • - (void)viewDidLoad { [super viewDidLoad]; self.refreshControl = [[UIRefreshControl alloc] init]; [self.refreshControl addTarget:self action:@selector(loadPage) forControlEvents:UIControlEventValueChanged]; [self.webView.scrollView addSubview:self.refreshControl]; }
      【解决方案5】:

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

      【讨论】:

      • +1 奇妙而巧妙的解决方案!完美运行 - 事实上,它应该完美UIScrollView 的子类一起使用
      • 是的! @RazorSharp 也是如此。
      【解决方案6】:

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

      【讨论】:

        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多