TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Aug 2017 |
Maintained by [zhengbo].
项目中我们会经常有加载网页的需求,所以UIWebView或者是WKWebView就是不可或缺的开发控件。iOS8以前用的是UIWebView,但是用过的同学就会发现UIWebView占用的内存较多,达到峰值时更是夸张。iOS8以后苹果推出了一个新的控件——WKWebView。据官方介绍,WKWebView的性能优化较UIWebView而言提高了数倍,查看内存占用率确实比UIWebView降低了不少。虽然WKWebView也有不少的缺点,但是与UIWebView过高占用内存相比,开发者们更愿意选择WKWebView。所以,随着iOS7的淘汰,WKWebView替换UIWebView成为了一种趋势。
开发中可能会有添加头部视图或尾部视图的需求,但是我们并不能像UITableView和UICollectionView那样便捷的添加头视图或尾视图。
pod 'ZBWKWebView'
UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
headerView.backgroundColor = [UIColor redColor];
UIGraphicsBeginImageContextWithOptions(headerView.bounds.size, NO, 0);
[headerView drawRect:headerView.bounds];
NSString *headerStr = @"I am headerView";
[headerStr drawAtPoint:CGPointMake(10, headerView.bounds.size.height * 0.5 - 30) withAttributes:@{NSFontAttributeName : [UIFont italicSystemFontOfSize:45], NSForegroundColorAttributeName : [UIColor whiteColor]}];
UIImage *headerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
headerView.image = headerImage;
//是否设置自定义headerView的背景色与WKWebView一致
_webView.isSameColorWithHeaderView = NO;
//设置头部视图
_webView.headerView = headerView;
UIImageView *footerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];
footerView.backgroundColor = [UIColor yellowColor];
UIGraphicsBeginImageContextWithOptions(footerView.bounds.size, NO, 0);
[footerView drawRect:footerView.bounds];
NSString *headerStr = @"I am footerView";
[headerStr drawAtPoint:CGPointMake(20, footerView.bounds.size.height * 0.5 - 30) withAttributes:@{NSFontAttributeName : [UIFont italicSystemFontOfSize:45], NSForegroundColorAttributeName : [UIColor redColor]}];
UIImage *headerImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
footerView.image = headerImage;
//是否设置自定义footerView的背景色与WKWebView一致
_webView.isSameColorWithFooterView = NO;
//设置尾部视图
_webView.footerView = footerView;