在 Swift 中以编程方式创建 webView [英] Creating webViews programmatically in Swift

查看:22
本文介绍了在 Swift 中以编程方式创建 webView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 ViewController 中创建类似图片库的东西.为此,我需要多个 webView,具体取决于我从 JSON 请求中获取的图像数量.如果需要,如何插入新的 webView?

I want to create something like an image gallery in my ViewController. For this, I'd need multiple webViews, depending on the number of images that I get from a JSON request. How can I insert new webViews if I need to?

如上图所示,我在 ViewController 中有一个 scrollView 和一个 UIWebView.如有必要,我将如何在第一个(第二个、第三个等)中创建一个新的 webView?可能吗?

As you can see in the above image, I have a scrollView and one UIWebView inside of the ViewController. How would I create a new webView inside of the first(second, third, etc.) if necessary? Is it possible?

推荐答案

使用这段代码,您可以尽可能简单地以编程方式创建 Web 视图

you can create the web view programatically as simple as possible use this piece of code

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)
}

如果你想使用这个委托函数

and if you want use this delegate function

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")
}

这篇关于在 Swift 中以编程方式创建 webView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆