[背景]
折腾:
期间,要把已经可以显示的web页面的这个web的view,添加到当前的subview中。
[折腾过程]
1.搜:
swift web view into subviews
参考:
2.然后期间还需要去研究:
3.自己去摸索:
搜:
swift WKWebView addSubview
参考:
好像需要:
自己创建一个rect的frame,然后再把WKWebView加进去?
但是之前就试过:
let webView = WKWebView() //let requestURL = NSURL(fileURLWithPath: REGISTRATION_AGREEMENT_URL) let requestURL = NSURL(string: "http://www.baidu.com") let request = NSURLRequest(URL: requestURL!) //webView.loadRequest(request) //self.webView!.loadRequest(request) webView.loadRequest(request) self.view.addSubview(webView) } override func loadView() { super.loadView() //self.webView = WKWebView() //self.view = self.webView! }
结果还是不行:
页面都加载不出来了:
import UIKit import WebKit class RegistrationAgreementController : UIViewController { //var webView: WKWebView? override func viewDidLoad() { super.viewDidLoad() //let webView = UIWebView() //self.view.addSubview(webView) let webView = WKWebView() webView.frame = CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60) //let requestURL = NSURL(fileURLWithPath: REGISTRATION_AGREEMENT_URL) let requestURL = NSURL(string: "http://www.baidu.com") let request = NSURLRequest(URL: requestURL!) //webView.loadRequest(request) //self.webView!.loadRequest(request) webView.loadRequest(request) self.view.addSubview(webView) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } }
效果是:
[总结]
此处,最终算是,基于原先核心代码:
letwebView =WKWebView() let requestURL = NSURL(string: "http://www.baidu.com") let request = NSURLRequest(URL: requestURL!) webView.loadRequest(request) self.view.addSubview(webView)
之后,再去添加了CGRectMake,即可,变成:
let webView = WKWebView() webView.frame = CGRectMake(0, 60, self.view.frame.size.width, self.view.frame.size.height - 60) let requestURL = NSURL(string: "http://www.baidu.com") let request = NSURLRequest(URL: requestURL!) webView.loadRequest(request) self.view.addSubview(webView)
即可使得支持:
把WKWebView添加到当前的view的subview中,指定的大小,去显示网页内容了。