我想在我的 ViewController 中创建类似图片库的东西。为此,我需要多个 webView,具体取决于我从 JSON 请求中获得的图像数量。如果需要,如何插入新的 webView?
正如您在上图中看到的,我在 ViewController 中有一个 scrollView 和一个 UIWebView。如有必要,我将如何在第一个(第二个、第三个等)内部创建一个新的 webView?可能吗?
最佳答案
您可以使用这段代码尽可能简单地以编程方式创建 Web View
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)
}
如果你想使用这个委托(delegate)函数
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")
}
关于ios - 在 Swift 中以编程方式创建 webView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31650816/