關於UIWebView設定高度自適應的問題

來源:互聯網
上載者:User

標籤:

- (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設定高度自適應的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.