gpt4 book ai didi

ios - AVPlayerViewController 在 iOS10 的弹出窗口中弄乱了底层模态视图 Controller

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:36:21 25 4
gpt4 key购买 nike

我有一个应用程序(支持界面方向 - 仅限纵向)具有下一个层次结构的模态呈现 View Controller :

A -> B -> AVP

A 是位于标签栏 Controller 中的 View Controller ,标签栏 Controller 又是窗口的根。

B 是一个相当简单的 View Controller ,带有按钮、图像和标签,但显示为弹出窗口:

// ... presentation method in A
let B = // create B
B.modalPresentationStyle = .popover
B.preferredContentSize = CGSize(width: 300, height: 400)
B.isModalInPopover = true
if let BPopover = B.popoverPresentationController {
BPopover.delegate = self
BPopover.permittedArrowDirections = []
let window = // grab current window
BPopover.sourceView = window
BPopover.sourceRect = window.bounds
BPopover.passthroughViews = nil
}
self.tabBarController?.present(B, animated: true, completion: nil)

AVP 是 B 提供的 AVPlayerViewController:

// This method is in B.
@IBAction func playVideoButtonPressed(_ sender: Any) {
if let videoURL = self.videoURL {
let videoPlayer = AVPlayer(url: videoURL)
let videoVC = AVPlayerViewController()
videoVC.player = videoPlayer
self.present(videoVC, animated: true, completion: nil)
}
}

在 iOS 10.0 上,如果我执行后续步骤,我会遇到问题:

  1. 现任副总裁。
  2. 旋转设备以便 AVP 水平旋转视频
  3. 我在水平方向关闭 AVP(使用其系统提供的控件)

当我回来时,我的 View Controller B 被弄乱了——移到了窗口的顶部并且它的尺寸变小了(内部也被弄乱了,但我猜内部由于我的自动布局约束而被弄乱了)。

这在 iOS 11 上似乎没有发生。

有什么办法可以解决吗?

编辑:按要求截图(出于隐私原因隐藏了标签栏):

Normal state of popup Popup after video player

附加信息:

我还拦截了一个委托(delegate)回调以获取更多信息:

func popoverPresentationController(_ popoverPresentationController: UIPopoverPresentationController,
willRepositionPopoverTo rect: UnsafeMutablePointer<CGRect>,
in view: AutoreleasingUnsafeMutablePointer<UIView>) {
print("willRepositionPopoverTo")
print(popoverPresentationController)
print(rect.pointee)
print(view.pointee)
}

打印 view 大小为 (w: 568; h: 320) 所以当我旋转 AVP Controller 时它似乎改变了我的应用程序的窗口方向,这导致调整大小我的弹出窗口。虽然它不会尝试重新调整大小 :( 在我关闭 AVP 之后。

最佳答案

我已经成功重现了您的问题,所以您可以高枕无忧,因为您知道这不仅仅是您的问题。我还花了相当多的时间尝试使用各种“黑客”来修复 iOS 10 的行为。我最初并不成功。此外,似乎尽管 iOS 11 解决了弹出窗口的定位问题,但它也引入了标签栏大小的错误(图片)。所以我的解决方案也需要解决这个问题。

enter image description here : enter image description here

更新的解决方法

在重新审视这个问题后,我重新考虑了一个我最初排除的解决方案。事实证明,AVPlayerViewController 只会影响其父级 UIWindow 实例的方向。使用额外的 UIWindow 并不是 iOS 中经常使用的解决方案,但在这里效果很好。

解决方案是创建一个清晰的 UIWindow 并带有填充 rootViewController,其唯一目的是在第二次出现时在没有动画的情况下关闭自身.此解决方案在 iOS 10 和 iOS 11 上同样适用。它不会改变用户体验(除了修复错误)。

步骤:

  1. 创建 Vanilla UIWindow 并使其背景清晰
  2. 将窗口的 rootVC 设置为 ShimVC 的一个实例>
  3. 使窗口成为关键且可见(因为一切都很清楚,用户什么也看不到)
  4. 从窗口的 rootVC 中呈现 AVPlayerViewController(这样你就可以得到动画,就像你不这样做一样)
  5. 当视频播放器关闭时,shim View Controller 将自行关闭。当一个窗口的 rootVC 自行关闭时,该窗口将被删除,原来的主窗口将再次成为关键。

这是一个 gif 动图,展示了它的效果:

enter image description here

class FirstViewController: UIViewController, UIViewControllerTransitioningDelegate {

@IBAction func popover(_ sender: UIButton) {

let b = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "B") as! B
b.modalPresentationStyle = .popover
b.preferredContentSize = CGSize(width: 300, height: 400)
b.isModalInPopover = true

if let ppc = b.popoverPresentationController {
ppc.delegate = self
ppc.permittedArrowDirections = []
let window = view.window!
ppc.sourceView = window
ppc.sourceRect = window.frame
ppc.passthroughViews = nil
}

present(b, animated: true, completion: nil)
}
}

extension FirstViewController: UIPopoverPresentationControllerDelegate {

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}

class ShimVC: UIViewController {

var appearances: Int = 0

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

if appearances > 0 {
// When the rootViewController of a window dismisses, that window
// gets removed from the view hiearchy and discarded, making the
// the previous window key automatically
dismiss(animated: true)
}

appearances += 1
}
}

class B: UIViewController {

let videoURL: URL? = Bundle.main.url(forResource: "ImAfraidWeNeedToUseMath", withExtension: "m4v")

@IBAction func playVideo(_ sender: UIButton) {

if let videoURL = self.videoURL {
let videoPlayer = AVPlayer(url: videoURL)
let videoVC = AVPlayerViewController()
videoVC.player = videoPlayer

let vc = ShimVC(nibName: nil, bundle: nil)

let videoWindow = UIWindow()
videoWindow.backgroundColor = .clear
videoWindow.rootViewController = vc
videoWindow.makeKeyAndVisible()

// Present the `AVPlayerViewController` from the root
// of the window
vc.present(videoVC, animated: true)
}
}

@IBAction func done(_ sender: UIButton) {
dismiss(animated: true)
}
}

最初提出的解决方法

我将提出一个解决方法。这是我之前在某些应用程序中看到的行为。基本上,不是从弹出窗口中显示视频播放器,而是使用 AVC 切换出 B。可能还有其他解决方案需要更多偏离现有 UIKit 功能。 (例如可能实现您自己的呈现 Controller 来实现弹出窗口或将弹出窗口实现为 subview Controller ,以便您可以直接从 A 呈现 AVC。)

在下面的解决方案中,A -> B 然后当用户按下播放器 B 时通知 A 它需要呈现 AVC。然后你得到 A -> AVC。当 AVC 被关闭时,我重新呈现 B,因此您返回到 A -> B。总体而言,在 iOS 10 或 iOS 11 上都没有 UI 问题,但您的用户将需要多等几分之一秒。如果时间紧迫,您可以禁用(或尝试缩短?)动画,但总的来说它感觉非常流畅和自然。

此外,我建议提交有关标签栏大小问题的雷达。

这是我的解决方案:

class A: UIViewController, UIViewControllerTransitioningDelegate {

var popover: B?

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

if let popover = popover {
if let ppc = popover.popoverPresentationController {
ppc.delegate = self
ppc.permittedArrowDirections = []
let window = view.window!
ppc.sourceView = window
ppc.sourceRect = window.frame
ppc.passthroughViews = nil
}

present(popover, animated: true, completion: nil)
}
}

@IBAction func popover(_ sender: UIButton) {

let b = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "B") as! B
b.modalPresentationStyle = .popover
b.preferredContentSize = CGSize(width: 300, height: 400)
b.isModalInPopover = true
b.delegate = self

if let ppc = b.popoverPresentationController {
ppc.delegate = self
ppc.permittedArrowDirections = []
let window = view.window!
ppc.sourceView = window
ppc.sourceRect = window.frame
ppc.passthroughViews = nil
}

self.popover = b

present(b, animated: true, completion: nil)
}
}

extension A: UIPopoverPresentationControllerDelegate {

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
}

extension A: BDelegate {

func dismissB() {
popover?.dismiss(animated: true)
popover = nil
}

func showAVPlayerViewController(_ vc: AVPlayerViewController) {

popover?.dismiss(animated: true) {
// Dispatch async allows it to come up in landscape if the phone is already rotated
DispatchQueue.main.async {
self.present(vc, animated: true)
}
}
}
}

protocol BDelegate: class {

func showAVPlayerViewController(_ vc: AVPlayerViewController)
func dismissB()
}

class B: UIViewController {

weak var delegate: BDelegate?

let videoURL: URL? = Bundle.main.url(forResource: "ImAfraidWeNeedToUseMath", withExtension: "m4v")

@IBAction func playVideo(_ sender: UIButton) {

if let videoURL = self.videoURL {
let videoPlayer = AVPlayer(url: videoURL)
let videoVC = AVPlayerViewController()
videoVC.player = videoPlayer
delegate?.showAVPlayerViewController(videoVC)
}
}

@IBAction func done(_ sender: UIButton) {
delegate?.dismissB()
}
}

关于ios - AVPlayerViewController 在 iOS10 的弹出窗口中弄乱了底层模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48667855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com