ios - iOS应用程序中的youtube iFrame在全屏后导致半屏变黑

标签 ios swift iframe youtube

我的WKWebView中有一个UIViewController,并且正在将youtube视频(使用iFrame)与文本一起嵌入到html中。当我启动视频时,它会按预期执行,并以全屏模式打开,我可以旋转风景方向,正常播放等。问题出在关闭视频时。我的应用程序被锁定为人像,并且从视频返回时,该应用程序是半黑屏,一半是我的应用程序,一半是我的应用程序的 View 看起来是横向的(宽度太大)。

我的应用程序在整个应用程序的Info.plist中被锁定为人像。视频旋转效果很好,只会引起这种有趣的结果。

1. Launch WKWebView with youtube iframe
2. Click to launch video (video plays full screen)
3. Rotate device to either landscape rotations.
4. Close the player
This is where you notice that half the application is black and the other half looks to be portrait orientation in landscape layout.

当我检查前后的 View 时,它们仿佛应用已旋转至横向模式,但设备处于纵向模式。 ( View 始于414x862。观看并旋转视频后,返回到显示为862x414的 View )

我不太确定这是怎么回事。有什么想法吗?

提前致谢

最佳答案

我能够找到解决方法/破解/解决方案。我将应用程序锁定为纵向,但为允许的方向覆盖了AppDelegate方法。

class AppDelegate: UIResponder, UIApplicationDelegate {

    ...

    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return OrientationManager.allowedOrientations
    }

    ...

}

我创建了一个OrientationManager,它只是一个帮助器类,允许我强制旋转并更改允许的方向。

class OrientationManager {
    /// The currently allowed orientations of the application (default: .portrait)
    static var allowedOrientations: UIInterfaceOrientationMask = .portrait

    /**
     * Method to force the current orientation of the device.
     *
     * - Parameter orientation: The orientation to change to.
     *
     * - Warning: This method uses setValue(_, forKey:) which accesses values that may change in the future
     */
    static func forceOrientation(_ orientation: UIInterfaceOrientation) {
        let orientationRawValue = orientation.rawValue
        UIDevice.current.setValue(orientationRawValue, forKey: "orientation")
        UINavigationController.attemptRotationToDeviceOrientation()
    }
}

我需要做的最后一件事是弄清楚视频何时进入和退出全屏状态。我发现当视频进入和退出全屏模式时,似乎确实可以触发某些通知。发生这种情况时,我将启用 View 旋转功能,使其可以在全屏视频后转到“横向”。视频关闭后,我的 View 现在(不幸地)以横向显示,然后我可以强制方向并将其重新锁定为纵向。

在带有Web View 的UIViewController中:

class WebViewWithVideoViewController: UIViewController {

    ...
    // MARK: Properties
    var videoDidFullScreenNotification: NSObjectProtocol?
    var videoDidMinimizeNotification: NSObjectProtocol?
    ...

    // MARK: Lifecycle
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        self.beginObserving()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        self.endObserving()
    }

    // MARK: Observers

    func beginObserving() {
        // This is a solution to the video causing a half black screen when the device is rotated to landscape.
        // The outline of the solution is that when the video is put into full screen mode, we update the
        // available orientations to include both landscape directions. Upon the video being closed,
        // we force the device orientation back to portrait and then lock it back to portrait only.
        // This seems to work because the UIViewController is actually in landscape once complete
        // and then it animates back to the portrait orientation and seems to keep the proper ratios
        // and sizes.
        if self.videoDidFullScreenNotification == nil {
            self.videoDidFullScreenNotification = NotificationCenter.default.addObserver(
                forName: UIWindow.didBecomeVisibleNotification,
                object: self.view.window,
                queue: nil
            ) { notification in
                OrientationManager.allowedOrientations = .allButUpsideDown
            }
        }

        if self.videoDidMinimizeNotification == nil {
            self.videoDidMinimizeNotification = NotificationCenter.default.addObserver(
                forName: UIWindow.didBecomeHiddenNotification,
                object: self.view.window,
                queue: nil
            ) { notification in
                OrientationManager.forceOrientation(.portrait)
                OrientationManager.allowedOrientations = .portrait
            }
        }
    }

    func endObserving() {
        if let videoDidFullScreenNotification = self.videoDidFullScreenNotification {
            NotificationCenter.default.removeObserver(videoDidFullScreenNotification)
        }

        if let videoDidMinimizeNotification = self.videoDidMinimizeNotification {
            NotificationCenter.default.removeObserver(videoDidMinimizeNotification)
        }
    }

    ...
}

这似乎解决了嵌入视频进入和退出全屏后屏幕显示半黑的问题。不幸的是,当您从全屏视频中返回时,会有一个轻微的动画,但这是一个非常奇怪的错误的一个小牺牲。

希望这对其他人(或类似问题)和愉快的编码有所帮助!

关于ios - iOS应用程序中的youtube iFrame在全屏后导致半屏变黑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59722098/

相关文章:

ios - SceneKit 中的场景渲染不正确

iphone - 内存使用随着 CTFontCreateWithName 和 CTFramesetterRef 的增加而增长

ios - 在 View 之间滑动时更改标签文本颜色

ios - 用于保管箱或谷歌驱动器的 DocumentViewController

html - 是否可以设置 iFrame 的样式

cordova - 无法使用 cordova iOS 在 iframe 中正确加载外部 URL

ios - 向我的项目添加核心数据时无法添加 sql 文件

iOS 8.x Swift MPRemoteCommandCenter - 禁用音频播放控件

ios - 快速登录

iframe - facebook like box 外部 css