ios 在 Swift 中以编程方式创建 webView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31650816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating webViews programmatically in Swift
提问by Orkhan Alizade
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 中创建类似图片库的东西。为此,我需要多个 webView,具体取决于我从 JSON 请求中获取的图像数量。如果需要,如何插入新的 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?
如上图所示,我在 ViewController 中有一个 scrollView 和一个 UIWebView。如有必要,我将如何在第一个(第二个、第三个等)中创建一个新的 webView?是否可以?
回答by Deepakraj Murugesan
you can create the web view programatically as simple as possible use this piece of code
您可以使用这段代码以尽可能简单的方式以编程方式创建 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)
}
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")
}
回答by kurrodu
For Swift 3:
对于 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)
}
Reference: Based on the answer provided by @Deepakraj Murugesan
参考:基于@Deepakraj Murugesan 提供的答案
回答by budidino
Although UIWebView is probably not the thing you should use for displaying images, you can create one with just a few lines of code:
虽然 UIWebView 可能不是你应该用来显示图像的东西,你可以用几行代码创建一个:
let webView = UIWebView(frame: someFrame)
webView.loadRequest(NSURLRequest(URL: someURL!))
view.addSubview(webView)
回答by Burning
you can't do that, Originally,webview will take up a lot of memory, if you use some quantity of it, can lead to a bad experience, why do not use ImageView?
你不能这样做,本来,webview 会占用大量内存,如果你使用它的一些数量,会导致糟糕的体验,为什么不使用 ImageView?
回答by John Lee
I think SDWebImage + UIImageView will help you. See: https://github.com/rs/SDWebImage
我认为 SDWebImage + UIImageView 会帮助你。见:https: //github.com/rs/SDWebImage
回答by Ben
For Swift 4:
对于 Swift 4:
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let url = URL(string: "https://www.earthhero.org")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
Plus you need to import WebKit and add WKNavigationDelegate to the Class.
另外,您需要导入 WebKit 并将 WKNavigationDelegate 添加到类。