import UIKit
import WebKit
class MLWebViewHeightViewController: MLBaseViewController {
var urlString: String?
@IBOutlet weak var testScrollView: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.urlString = "https://news.cnblogs.com/n/588790/"
self.buildUI()
}
func buildUI() {
self.testScrollView.addSubview(self.changeWebView)
self.testScrollView.addSubview(self.testLabel)
self.testLabel.snp.makeConstraints { [unowned self] (make) in
make.width.equalToSuperview()
make.height.equalTo(60.0)
make.top.equalTo(self.changeWebView.snp.bottom)
}
self.testScrollView.sizeToFit()
if let webUrlString = self.urlString {
if let encodedStr = webUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
if let myUrl = URL(string: encodedStr) {
let myRequest = URLRequest(url: myUrl)
self.changeWebView.load(myRequest)
}
}
}
}
// MARK: - 懒加载
lazy var changeWebView: WKWebView = {
let webConfiguration = WKWebViewConfiguration()
//初始化偏好设置属性:preferences
webConfiguration.preferences = WKPreferences()
//是否支持JavaScript
webConfiguration.preferences.javaScriptEnabled = true
//不通过用户交互,是否可以打开窗口
webConfiguration.preferences.javaScriptCanOpenWindowsAutomatically = false
let webFrame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 0)
let webView = WKWebView(frame: webFrame, configuration: webConfiguration)
webView.backgroundColor = UIColor.blue
webView.navigationDelegate = self
webView.scrollView.isScrollEnabled = false
webView.scrollView.bounces = false
webView.scrollView.showsVerticalScrollIndicator = false
webView.scrollView.showsHorizontalScrollIndicator = false
return webView
}()
lazy var testLabel: UILabel = {
let tempLabel = UILabel()
tempLabel.text = "我是一个Label"
tempLabel.textAlignment = .center
tempLabel.backgroundColor = UIColor.red
return tempLabel
}()
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension MLWebViewHeightViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
var webheight = 0.0
// 获取内容实际高度
self.changeWebView .evaluateJavaScript("document.body.scrollHeight") { [unowned self] (result, error) in
if let tempHeight: Double = result as? Double {
webheight = tempHeight
print("webheight: \(webheight)")
}
DispatchQueue.main.async { [unowned self] in
var tempFrame: CGRect = self.changeWebView.frame
tempFrame.size.height = CGFloat(webheight)
self.changeWebView.frame = tempFrame
self.testScrollView.contentSize = CGSize(width: self.testScrollView.frame.size.width, height: self.changeWebView.frame.size.height + self.testLabel.frame.size.height)
}
}
}
}
swift WKWebview获取内容高度自适应
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 关于WebView内容高度的获取,相信很多人都踩过坑,无法获取到准确高度,导致页面布局出现差错,搜到的资料很多但都...
- 在这个问题卡了很久,最终参照了http://www.jianshu.com/p/6bbcc438b188 作者...