Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating webViews programmatically in Swift

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?

enter image description here

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?

like image 956
Orkhan Alizade Avatar asked Jul 27 '15 10:07

Orkhan Alizade


People also ask

How do I add WKWebView to my storyboard?

First add the web view as a UIWebView outlet in the storyboard / IB. This will give you a property like this: @property (weak, nonatomic) IBOutlet UIWebView *webView; Then just edit your code to change it to a WKWebView.

How to create a WebView sample in Xcode?

Step 1 − Open Xcode and create a single view application and name it WebViewSample. Step 2 − Open ViewController.swift file and import the WebKit module. import WebKit Step 3 − Add a property of WebKit in ViewController.swift. var webView: WKWebView!

What is WebView in iOS?

As per Apple, − It is an object that displays interactive web content, such as for an in-app browser. So in this post, we will be seeing how to create WebView and load the data in it.

How do I create a custom view programmatically?

Let’s create a custom view programmatically! Create an object to represent your new view by declaring a variable or type UIView in your viewController class. I’ve named mine “blackSquare”.

How to create a custom UIView programmatically?

Let’s create a custom view programmatically! Create an object to represent your new view by declaring a variable or type UIView in your viewController class. I’ve named mine “blackSquare”. 2. Form your view by defining a position fo r it with x, y coordinate values and by defining a size for it with width and height values.


2 Answers

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")
}
like image 89
Deepakraj Murugesan Avatar answered Sep 18 '22 15:09

Deepakraj Murugesan


For 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

like image 36
kurrodu Avatar answered Sep 18 '22 15:09

kurrodu