首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为UIWebView使用UIRefreshControl

为UIWebView使用UIRefreshControl
EN

Stack Overflow用户
提问于 2012-11-14 21:33:58
回答 8查看 15K关注 0票数 14

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

代码语言:javascript
复制
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    [rabih addSubview:rabih];
EN

回答 8

Stack Overflow用户

发布于 2013-05-03 04:36:29

下面是使用下拉菜单刷新UIWebview的方法:

代码语言:javascript
复制
- (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];
}
票数 49
EN

Stack Overflow用户

发布于 2015-05-07 22:11:14

Swift中使用这个,

假设您希望在WebView中使用pull来刷新,

所以试试这段代码:

代码语言:javascript
复制
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中,我使用了这个:

代码语言:javascript
复制
- (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];
}
票数 9
EN

Stack Overflow用户

发布于 2013-04-08 01:46:09

我已经非常快速地将以下代码添加到我的代码中:

代码语言:javascript
复制
[webserver.scrollView addSubview:refreshControl]

所以看起来UIWebView本身就有一个UIScrollView,你可以直接连接到它上。希望它对你有用。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13379815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档