如何让 WKWebView 在后台显示

How to get WKWebView to show in background

我正在尝试让动画 gif 在后台播放,并在顶部显示一些标签和按钮。此代码运行并显示动画 gif,但不显示按钮或标签文本。

import Foundation
import UIKit
import WebKit

class LoginViewController: UIViewController {

  @IBOutlet var containerView: UIView! = nil
var webViewBG: WKWebView?

  @IBOutlet weak var loginButton: UIButton!
  @IBOutlet weak var signUpButton: UIButton!

    override func loadView() {
        super.loadView()
        self.webViewBG = WKWebView()
        self.view = self.webViewBG

    }

    override func viewDidLoad() {

        super.viewDidLoad()

        let htmlPath = Bundle.main.path(forResource: "WebViewContent", ofType: "html")
        let htmlURL = URL(fileURLWithPath: htmlPath!)
        let html = try? Data(contentsOf: htmlURL)

        var req = NSURLRequest(url:htmlURL)
        self.webViewBG!.load(req as URLRequest)
        webViewBG?.isUserInteractionEnabled = false;

        self.loginButton.layer.borderColor = UIColor.white.cgColor
        self.loginButton.layer.borderWidth = 2

        self.signUpButton.layer.borderColor = UIColor.white.cgColor
        self.signUpButton.layer.borderWidth = 2        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }

    override var preferredStatusBarStyle : UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }

}

看起来这一行覆盖了你的视图控制器的视图,其中按钮是:

self.view = self.webViewBG

尝试

self.view.addSubview(self.webViewBG)

您可能还需要调整 z 位置,以便 webView 位于 'behind' 按钮,如下所示:

self.webViewBG.layer.zPosition = 0.5

更高的 zPosition 意味着视图更突出 'towards' 用户