Use UIRefreshControl for UIWebView

前端 未结 8 783
花落未央
花落未央 2021-02-01 08:24

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 We

相关标签:
8条回答
  • 2021-02-01 08:52

    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];
    }
    
    0 讨论(0)
  • 2021-02-01 08:54

    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.

    0 讨论(0)
  • 2021-02-01 08: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.

    0 讨论(0)
  • 2021-02-01 08:58

    In Swift use this,

    Let's assume you wants to have pull to refresh in 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:

    - (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];
    }
    
    0 讨论(0)
  • 2021-02-01 08:59

    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.

    0 讨论(0)
  • 2021-02-01 09:07

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

    - (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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题