標籤:
- (void)viewDidLoad{ [super viewDidLoad]; _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 66, 320, 480)]; //中間還有其他控制項
//建立webView時要設定一個臨時的frame,不然的話webViewDidFinishLoad代理方法中設定webView高度自適應之後,webView會有很多空白 _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 100, 320, 10)]; _webView.delegate = self; //_webView.ScalesPageToFit = YES;//可以用捏合手勢來放大或縮小webView頁面 _webView.scrollView.scrollEnabled = NO;//禁止UIWebView滾動 //WebView載入本地的html資料 NSMutableDictionary *dict = [LoadFileTools loadWebViewContentsWithFileName:kJobDescribe]; NSString *filePath = [dict objectForKey:@"filePath"]; NSString *htmlString = [dict objectForKey:@"htmlString"]; [_webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:filePath]];}- (void)webViewDidFinishLoad:(UIWebView *)webView{
//擷取WebView內容的高度(貌似這兩種方式都行)
//NSString *contentH = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
NSString *contentH = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];
//計算座標 CGFloat webViewY = otherControlY + 50; CGFloat webViewH = [contentH floatValue] + 25; webView.frame = CGRectMake(12, webViewY, 320 - 12, webViewH); //禁止選中 [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitUserSelect=‘none‘;"]; //禁止快顯功能表 [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.style.webkitTouchCallout=‘none‘;"]; //設定捲動區域 _scrollView.contentSize = CGSizeMake(0, webViewY + webViewH - 10); [_scrollView addSubview:_webView]; [self.view addSubview:_scrollView];}
關於UIWebView設定高度自適應的問題