为什么 WKWebView 不显示视频?- 斯威夫特 3 [英] Why WKWebView doesn't display videos? - Swift 3

查看:52
本文介绍了为什么 WKWebView 不显示视频?- 斯威夫特 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以编程方式在 viewController(在 viewDidLoad() 中)成功添加了一个 WKWebView.当加载包含视频的 url 时,它看起来很好,但是当我尝试点击它(播放它)时,我看不到它,但音频工作正常.

I successfully added a WKWebView programmatically In a viewController (in viewDidLoad()). When loading a url that contains a video it appears fine, but when I try to tap on it (playing it), I cannot see it, but the audio works fine.

奇怪的是我创建了一个新项目只是为了确保它可以正常工作并且确实如此,我复制了 webView 显示视频的完全相同的代码.

The weird thing is I created a new project just to make sure it should works fine and it did, I copied the same exact code the webView displayed the video as it should.

在转换为 Swift 3 之前运行良好.

It was working fine before converting to Swift 3.

这是点击视频时的样子:

This is how it looks when tapping on the video:

点击之前:

点击后:

我还尝试了另一个网页:

I also tried another web page:

点击之前:

点击后(注意状态栏现在是隐藏的):

简单地说,这是代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let web = WKWebView(frame: view.frame)
    let urlRequest = URLRequest(url: URL(string: "http://www.w3schools.com/html/html5_video.asp")!)       
    web.frame = view.frame
    web.load(urlRequest)

    view.addSubview(web)
 }

我尝试检查了许多没有任何输出的情况.我错过了什么?

I tried to check many cases without any output. What am I missing?

提前致谢.

推荐答案

我认为视频在您的 WKWebview 后面播放,您能否调试视图层次结构并发布它?Xcode->Menu->Debug->View Debugging->Capture View Hierarchy 播放视频时.

I think the video is playing behind your WKWebview, could you please debug the view hierarchy and post it? Xcode->Menu->Debug->View Debugging->Capture View Hierarchy when playing the video.

我已经尝试了一个新的 swift3 项目和你的代码,没有问题,这是视图层次结构:

I've tried with a new swift3 project and with your code, there is no problem, here is the view hierarchy:

您可以从左侧面板看到,AVPlayerView 在另一个 UIWindow 中,与 WKWebView 不同,所以我猜您项目中包含 WKWebView 的 UIWindow 具有更高的 windowLevel,因此它显示在包含 AVPlayer 的 UIWindow 上方.

You can see that from left pannel, the AVPlayerView is in another UIWindow, different from WKWebView,So I guess the UIWindow which contains the WKWebView in your project has a higher windowLevel so it shows above the UIWindow which contains AVPlayer.

通过将默认 UIWindow 设置为更高的 WindowLevel(UIWindowLevelAlert),我重现了您在项目中看到的内容.

And by making the default UIWindow a higher WindowLevel(UIWindowLevelAlert),I reproduced what you've seen in your project.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window?.windowLevel = UIWindowLevelAlert
    return true
}

这篇关于为什么 WKWebView 不显示视频?- 斯威夫特 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆