2015-01-08 130 views
4

我一直在環顧四周,無法看到任何迅速相關的方式來做到這一點。我試圖讓我的UIWebViews高度是動態的。我有一個使用loadHtmlString函數加載數據的UIWebView。事情是我從sqlite數據庫加載數據,每次加載不同長度的字符串時,Web視圖自然會獲得不同的高度。
現在我需要知道如何使UIWebView的確切高度才能在webView下正確加載下一個內容。這是我迄今爲止UIWebView動態內容大小

var jobSkillView = UIWebView(frame: CGRectMake(-5, 480.0, screenWidth, 300.0)) 
jobSkillView.loadHTMLString("<html><body p style='font-family:arial;font-size:16px;'>" + jobSkills + "</body></html>", baseURL: nil) 
jobSkillView.stringByEvaluatingJavaScriptFromString("document.body.innerHTML") 
jobSkillView.scrollView.scrollEnabled = true 
jobSkillView.scrollView.bounces = true 
jobSkillView.sizeToFit() 
border.addSubview(jobSkillView) 

我發現像這樣的SO,​​但不知道如何將它鏈接到UIWebView的框架:

func webViewDidFinishLoad(jobSkillView : UIWebView){ 
    // Change the height dynamically of the UIWebView to match the html content 
    var jobSkillViewFrame: CGRect = jobSkillView.frame 
    jobSkillViewFrame.size.height = 1 
    jobSkillView.frame = jobSkillViewFrame 
    var fittingSize: CGSize = (jobSkillView.sizeThatFits(CGSizeZero)) 
    jobSkillViewFrame.size = fittingSize 
    // webViewFrame.size.width = 276; Making sure that the webView doesn't get wider than 276 px 
    jobSkillView.frame = jobSkillViewFrame 
    var jobSkillViewHeight = jobSkillView.frame.size.height 
} 

回答

23

所以這是一個非常棒的函數,你在那裏寫的,OP!
這裏只是一個短,更優雅的版本的代碼:

//make sure to declare the delegate when creating your webView (add UIWebViewDelegate to class declaration as well) 
myWebView.delegate = self 

func webViewDidFinishLoad(webView: UIWebView) { 
    webView.frame.size.height = 1 
    webView.frame.size = webView.sizeThatFits(CGSizeZero) 
} 

斯威夫特3

myWebView.delegate = self 

func webViewDidFinishLoad(_ webView: UIWebView) { 
    webView.frame.size.height = 1 
    webView.frame.size = webView.sizeThatFits(.zero) 
} 
+0

對於iphone 5和5s,webView內容不會增加 – Abhishek

+0

您確定您已正確執行了這些步驟嗎?因爲對我來說,它可以在所有設備上平等地運作 – LinusGeffarth

+0

是的,我有,因爲對於6及以上的工作正常 – Abhishek

1

好吧,我想通了,我在做什麼所以現在錯了,我知道如何調用這個函數。首先,我應該在我的課堂上調用一個UIWebViewDelegate。然後將我的var jobSkillView設置爲(jobSkillView.delegate = self)。現在,這是我犯了重大錯誤的地方。在我的代碼中,我有一個if else聲明,我扔了我的func webViewDidLoad。我應該把它從聲明中放到實際的類中,以覆蓋UIWebView的框架。說到框架****不要忘了將框架高度設置爲1.然後,只有這個功能將適用於您的UIWebView。之後,你應該爲你的UIWebView for Swift提供一個全功能的高度。

+0

可否請您發送驗證碼。我也面臨同樣的問題 – lazyCoder

+0

是啊生病看看我是否還有代碼! – Tom

6

這是我的自定義類與自定義UIWebViews一起工作,它包含了獲取正確的scrollview內容高度,設置UIWebView高度和創建自定義高度約束以獲得自動佈局工作的一切。它也加載一些自定義的CSS樣式...

class CustomUIWebView: UIWebView, UIWebViewDelegate { 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.scrollView.scrollEnabled = false 
     self.scrollView.bounces = false 
     self.delegate = self 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.scrollView.scrollEnabled = false 
     self.scrollView.bounces = false 
     self.delegate = self 
    } 

    override func loadHTMLString(string: String!, baseURL: NSURL!) { 
     var cssURL:String = NSBundle.mainBundle().pathForResource("webview", ofType: "css")! 
     var s:String = "<html><head><title></title><meta name=\"viewport\" content=\"initial-scale=1, user-scalable=no, width=device-width\" /><link rel=\"stylesheet\" href=\"./webview.css\" ></link></head><body>"+string+"</body></html>"; 
     var url:NSURL 

     if baseURL == nil { 
      url = NSBundle.mainBundle().bundleURL 
     } else { 
      url = baseURL 
     } 

     super.loadHTMLString(s, baseURL: url) 
    } 

    func webViewDidFinishLoad(webView: UIWebView) { 
     self.webViewResizeToContent(webView) 
    } 

    func webViewResizeToContent(webView: UIWebView) { 
     webView.layoutSubviews() 

     // Set to smallest rect value 
     var frame:CGRect = webView.frame 
     frame.size.height = 1.0 
     webView.frame = frame 

     var height:CGFloat = webView.scrollView.contentSize.height 
     println("UIWebView.height: \(height)") 

     webView.setHeight(height: height) 
     let heightConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: height) 
     webView.addConstraint(heightConstraint) 

     // Set layout flag 
     webView.window?.setNeedsUpdateConstraints() 
     webView.window?.setNeedsLayout() 
    } 

} 
+0

感謝您的代碼片段,它工作正常! – Tiois

+0

我不明白爲什麼isScrollEnabled設置爲false。 –

1

使用下面的代碼,使您的UIWebView高度斯威夫特3.X動態

func webViewDidFinishLoad(_ webView: UIWebView) {   

let height = webView.scrollView.contentSize.height 
var wRect = webView.frame 
wRect.size.height = height 
webView.frame = wRect 

} 
+0

在UIcollectionViewCell @TejaKumarBethina中不起作用,寬度超出屏幕並與其他視圖重疊 –

11

這隻適用於我

func webViewDidFinishLoad(_ webView: UIWebView) { 
    webView.frame.size.height = 1 
    webView.frame.size = webView.sizeThatFits(.zero) 
    webView.scrollView.isScrollEnabled=false; 
    myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height 
    webView.scalesPageToFit = true 
} 

確保你已經創建了一個出路myWebViewHeightConstraint

+0

與我一起工作但我在末尾添加了updateConstrain –

+0

謝謝@Gulz它真的工作對我來說... –

+0

@Gulz它不適用於UICollectionViewCell如何使它適用於UICollectionViewCell –

0

當比得到當時的變化Webview高度爲1字型變化適當offsetHeightWebview

webView1.frame.size.height = 1 
0

你可以使用這個類makking webview大小以適應swift 3和swift 4

// 
// RSWebView.swift 
// CustumWebViewDemo 
// 
// Created by Ruchin Somal on 01/01/18. 
// Copyright © 2018 Ruchin Somal. All rights reserved. 
// 

import UIKit 

class RSWebView: UIWebView, UIWebViewDelegate { 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder)! 
     self.scrollView.isScrollEnabled = false 
     self.scrollView.bounces = false 
     self.delegate = self 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.scrollView.isScrollEnabled = false 
     self.scrollView.bounces = false 
     self.delegate = self 
    } 

    override func loadHTMLString(_ string: String?, baseURL: URL?) { 
     let s:String = "<html><head><title></title><meta name=\"viewport\" content=\"initial-scale=1, user-scalable=no, width=device-width\" /><link rel=\"stylesheet\" href=\"./webview.css\" ></link></head><body>"+string!+"</body></html>"; 
     var url:URL 

     if baseURL == nil { 
      url = Bundle.main.bundleURL 
     } else { 
      url = baseURL! 
     } 

     super.loadHTMLString(s, baseURL: url) 
    } 

    func webViewDidFinishLoad(_ webView: UIWebView) { 
     self.webViewResizeToContent(webView: webView) 
    } 

    func webViewResizeToContent(webView: UIWebView) { 
     webView.layoutSubviews() 

     // Set to initial value of webview 
     var frame:CGRect = webView.frame 
     frame.size.height = 1.0 
     webView.frame = frame 

     // Calculated height of webview 
     let wvheight: CGFloat = webView.scrollView.contentSize.height 
     print("UIWebView.height: \(wvheight)") 
     var wvRect = webView.frame 
     wvRect.size.height = wvheight 
     webView.frame = wvRect 

     let heightConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.height, multiplier: 1.0, constant: wvheight) 
     webView.addConstraint(heightConstraint) 

     webView.window?.setNeedsUpdateConstraints() 
     webView.window?.setNeedsLayout() 
    } 

} 
1

我ha d問題與:

let height = webView.scrollView.contentSize.height 

有時我的網頁視圖高度根本沒有計算和保持默認值。 所以最後我設法找到更好的解決方案,工作正常,只是替換該行代碼:

let height = webView.stringByEvaluatingJavaScript(from: "document.body.scrollHeight") 

這是我最後的完整代碼:

func webViewDidFinishLoad(_ webView: UIWebView) { 

    //webview height 
    webView.frame.size.height = 1 
    webView.frame.size = webView.sizeThatFits(.zero) 
    webView.scrollView.isScrollEnabled = false 
    let height = webView.stringByEvaluatingJavaScript(from: "document.body.scrollHeight") 
    if let height = height { 
     if let heightInt = Int(height) { 
      let heightFloat = Float(heightInt) 

      webViewHeightConstraint.constant = CGFloat(heightFloat) 
     } 
    } 
    webView.scalesPageToFit = true 
}