Dee*_*san 23
您可以使用这段代码尽可能简单地以编程方式创建Web视图
override func viewDidLoad() {
super.viewDidLoad()
let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
webV.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.co.in")))
webV.delegate = self;
self.view.addSubview(webV)
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用这个委托功能
func webView(webView: UIWebView!, didFailLoadWithError error: NSError!) {
print("Webview fail with error \(error)");
}
func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
return true;
}
func webViewDidStartLoad(webView: UIWebView!) {
print("Webview started Loading")
}
func webViewDidFinishLoad(webView: UIWebView!) {
print("Webview did finish load")
}
Run Code Online (Sandbox Code Playgroud)
对于Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
let webV = UIWebView()
webV.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
webV.loadRequest(NSURLRequest(url: NSURL(string: "https://www.apple.com")! as URL) as URLRequest)
webV.delegate = self
self.view.addSubview(webV)
}
Run Code Online (Sandbox Code Playgroud)
参考:根据@Deepakraj Murugesan提供的答案
| 归档时间: |
|
| 查看次数: |
20929 次 |
| 最近记录: |