2015-01-08 131 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 
}