Use UIRefreshControl for UIWebView

前端 未结 8 767
花落未央
花落未央 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: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];
    }
    

提交回复
热议问题