pod 'ZBWKWebView'
在文件中引入頭文件:#import "ZBWKWebView.h"
創(chuàng)建headerView
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;
//是否設(shè)置自定義headerView的背景色與WKWebView一致
_webView.isSameColorWithHeaderView = NO;
//設(shè)置頭部視圖
_webView.headerView = headerView;
創(chuàng)建footerView
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;
//是否設(shè)置自定義footerView的背景色與WKWebView一致
_webView.isSameColorWithFooterView = NO;
//設(shè)置尾部視圖
_webView.footerView = footerView;
在代理方法中設(shè)置HeaderView和FooterView
//頁面開始加載時調(diào)用
- (void)webView:(WKWebView * _Nonnull)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
[_webView setupHeaderViewForWebView:(ZBWKWebView *)webView];
}
//頁面完成加載時調(diào)用
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
[_webView setupFooterViewForWebView:(ZBWKWebView *)webView];
}