0

I am attempting to display 5 web views and I would like the web views to be created in a for loop. The code I am currently using creates only one web view where it should create one for every url in pages. Any help would be appreciated!

import UIKit
import Foundation
import WebKit
import AVFoundation

class displayviews: UIViewController, WKUIDelegate {
    
    var pages = ["https://example.com", "https://example", "https://example", "https://example.com", "https://example.com"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        for i in pages{
            var inti = Int(i) ?? 0
            //var currentwebview = String("webView") + i
            let myWebView:WKWebView = WKWebView(frame: CGRect(x:0, y:CGFloat(inti*200), width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height/CGFloat(pages.count)))
            myWebView.uiDelegate = self
            self.view.addSubview(myWebView)
            //1. Load web site into my web view
            let myURL = URL(string: pages[inti])
            let myURLRequest:URLRequest = URLRequest(url: myURL!)
            myWebView.load(myURLRequest)
        }
    }
}
Claudio
  • 5,078
  • 1
  • 22
  • 33

1 Answers1

0

Your code is creating the 5 webviews, the problem is that they are being placed on top of each other and as such you can only see the top most one. You can either use a stackview like on the following example or set constraints on the webviews.

class ViewController: UIViewController, WKUIDelegate {
    
    var pages = ["https://example.com", "https://example.com", "https://example.com", "https://example.com", "https://example.com"]
    
    lazy var stackView: UIStackView = {
        let view = UIStackView()
        view.axis = .vertical
        view.translatesAutoresizingMaskIntoConstraints = false
        view.distribution = .fillEqually
        self.view.addSubview(view)
        NSLayoutConstraint.activate([
            view.leftAnchor.constraint(equalTo: self.view.leftAnchor),
            view.topAnchor.constraint(equalTo: self.view.topAnchor),
            view.rightAnchor.constraint(equalTo: self.view.rightAnchor),
            view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
        ])
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        for (inti, page) in pages.enumerated() {
            //var currentwebview = String("webView") + i
            let myWebView:WKWebView = WKWebView(frame: CGRect(x:0, y:CGFloat(inti*200), width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height/CGFloat(pages.count)))
            myWebView.uiDelegate = self
            //self.view.addSubview(myWebView)
            stackView.addArrangedSubview(myWebView)
            //1. Load web site into my web view
            let myURL = URL(string: page)
            let myURLRequest:URLRequest = URLRequest(url: myURL!)
            myWebView.load(myURLRequest)
        }
        
    }
}
Claudio
  • 5,078
  • 1
  • 22
  • 33
  • Thanks that worked! The only issue is that it loads the same webpage for all web views when in my real code it has multiple different websites in pages. Also is there a way to have the web views continue on downward instead of reducing in size? Thanks again for your help! – user16355394 Dec 23 '21 at 16:56
  • @user16355394 Strange behaviour, did you make sure you're not loading the same url? About the size yes it is possible, you can wrap the stackview on a scrollview and switch the bottom constraint of the stackview with a height constraint with a constant value greater than screen height. – Claudio Dec 27 '21 at 11:40
  • 1
    @user16355394 the `inti` is always zero. – Zas Jan 03 '22 at 07:23
  • @user16355394 Zas is correct, I've fixed it. – Claudio Jan 03 '22 at 09:21