可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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];
回答1:
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];
}
回答2:
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];
}
回答3:
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.
回答4:
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.
回答5:
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.
回答6:
Have a look at CKRefreshControl, which you may be able to customize to your needs.
回答7:
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];
}
}
回答8:
My answer is based on @Zaid Pathan answer but updated for 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()
}
Finally, don't forget to stop reloading:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if refreshController.isRefreshing{
refreshController.endRefreshing()
}
}