0.拉线引用
@property (weak, nonatomic) IBOutlet UIWebView *classDetail;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicatorView;
//滑动view的高度
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHigh;
1.加入代理协议
@interface CourseViewController ()<UIWebViewDelegate>{
float _webViewHeight;
float _loadFlag;
}
2.把webView设置代理
self.classDetail.delegate = self;
3.加入如下方法
//设置webView自动高度
#pragma mark webView代理方法
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.activityIndicatorView stopAnimating];
//根据网页高度
NSString * curHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
_webViewHeight = [curHeight floatValue];
_contentHigh.constant = _webViewHeight + 800;
//背景色白色
[self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
[self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#939393'"];
[self.tableView reloadData];
_loadFlag += 0.5;
if (_loadFlag == 1) {
[SVProgressHUD dismiss];
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.activityIndicatorView startAnimating];
}
注意 可以单独拉控件的约束来设置高度
_contentHigh.constant = _webViewHeight + 800;
还要刷新tableView
[self.tableView reloadData];
附带js语句,改变网页字体颜色和一些参数,好用啊。
//背景色白色
[self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFFFF'"];
//字体颜色
[self.webView
stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= '#939393'"];
//字体大小
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '330%'"];
//系统字体颜色
[webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'gray'"];
还有一些属性
self.myWebView.delegate =self;
self.myWebView.opaque =NO; //不设置这个值页面背景始终是白色
self.myWebView.backgroundColor = [UIColorclearColor];
self.myWebView.scalesPageToFit =NO; //禁止用户缩放页面
self.myWebView.dataDetectorTypes =UIDataDetectorTypePhoneNumber|UIDataDetectorTypeLink;
self.myWebView.scrollView.pagingEnabled =YES;
self.myWebView.scrollView.contentInset =UIEdgeInsetsMake(0,0,0,0);
self.myWebView.autoresizingMask =UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.myWebView];