如何让WKWebView在后台显示

时间:2018-03-30 16:41:32

标签: ios swift xcode ios11

我想在背景中播放动画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
    }

}

1 个答案:

答案 0 :(得分:0)

看起来这一行会覆盖视图控制器的视图,按钮位于:

self.view = self.webViewBG

尝试

self.view.addSubview(self.webViewBG)

您可能还需要调整z位置,以便webView位于按钮的“后面”,如下所示:

self.webViewBG.layer.zPosition = 0.5

更高的zPosition意味着视图更加“朝向”用户