Search code examples
iosswiftuiwebviewicarousel

Preview UIWebView in iCarousel in Swift


I am writing a app in Swift where I am trying to load a preview of images using iCarousel of html pages for which I am using UIWebView.

Now the problem I have is I am unable to preview html pages, however when I trying to preview images, I am able to preview it.

Here is my code

func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {

      var webView : UIImageView!
        let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, 200, 200))
         webV.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!))
        webV.contentMode = .ScaleAspectFit

        if webView == nil{
            webView = UIImageView(frame:CGRect(x:0,y:0,width:200,height:200))
            webView.contentMode = .ScaleAspectFit
        }else{
            webView = view as! UIImageView
        }

        webView.image = UIImage(named:"left-arrow.png")!

        return webView //returns image carousel
        //return webV //returns blank carousel. I want this to work !!!
    }

How do I achieve this functionality ?

Edit 1

I can preview images for carousel.type = iCarouselType.Linear screen attached. enter image description here This is how it looks

but I want it for any other carousel, say carousel.type = iCarouselType.CoverFlow this is what I get enter image description here This is what I get when I am returning UIImageView enter image description here


Solution

  • I have achieved this functionality by using WKWebView instead of UIWebView

    Here is the working code

        let webV:WKWebView!
        webV = WKWebView(frame: CGRectMake(20, 20, 200, 200))
        var url = NSURL(string:"http://www.fb.com/")
        var req = NSURLRequest(URL:url!)
        webV!.loadRequest(req)
        return webV
    

    Also,make sure you have imported WebKit by adding the below line

    import WebKit