【问题标题】:Create multiple webviews in a for loop swift在 for 循环中快速创建多个 webview
【发布时间】:2022-01-24 14:06:55
【问题描述】:

我正在尝试显示 5 个 Web 视图,并且我希望在 for 循环中创建 Web 视图。我目前使用的代码只创建一个 web 视图,它应该为页面中的每个 url 创建一个。任何帮助将不胜感激!

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)
        }
    }
}

【问题讨论】:

    标签: swift web view wkwebview wkwebviewconfiguration


    【解决方案1】:

    您的代码正在创建 5 个 webview,问题是它们被放置在彼此之上,因此您只能看到最上面的一个。您可以使用下面示例中的 stackview,也可以在 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)
            }
            
        }
    }
    

    【讨论】:

    • 感谢工作!唯一的问题是,当在我的真实代码中它在页面中有多个不同的网站时,它会为所有 Web 视图加载相同的网页。还有没有办法让网络视图继续向下而不是缩小尺寸?再次感谢您的帮助!
    • @user16355394 奇怪的行为,你确定你没有加载相同的网址吗?关于大小是可能的,您可以将stackview包装在滚动视图上,并将stackview的底部约束切换为具有大于屏幕高度的常数值的高度约束。
    • @user16355394 inti 始终为零。
    • @user16355394 Zas 是正确的,我已经修好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 2019-02-22
    • 2014-10-19
    • 1970-01-01
    相关资源
    最近更新 更多