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

查看:101
本文介绍了在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天全站免登陆