- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个应用程序(支持界面方向 - 仅限纵向)具有下一个层次结构的模态呈现 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 上,如果我执行后续步骤,我会遇到问题:
当我回来时,我的 View Controller B 被弄乱了——移到了窗口的顶部并且它的尺寸变小了(内部也被弄乱了,但我猜内部由于我的自动布局约束而被弄乱了)。
这在 iOS 11 上似乎没有发生。
有什么办法可以解决吗?
编辑:按要求截图(出于隐私原因隐藏了标签栏):
附加信息:
我还拦截了一个委托(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 解决了弹出窗口的定位问题,但它也引入了标签栏大小的错误(图片)。所以我的解决方案也需要解决这个问题。
在重新审视这个问题后,我重新考虑了一个我最初排除的解决方案。事实证明,AVPlayerViewController
只会影响其父级 UIWindow
实例的方向。使用额外的 UIWindow
并不是 iOS 中经常使用的解决方案,但在这里效果很好。
解决方案是创建一个清晰的 UIWindow
并带有填充 rootViewController
,其唯一目的是在第二次出现时在没有动画的情况下关闭自身.此解决方案在 iOS 10 和 iOS 11 上同样适用。它不会改变用户体验(除了修复错误)。
步骤:
UIWindow
并使其背景清晰ShimVC
的一个实例>AVPlayerViewController
(这样你就可以得到动画,就像你不这样做一样)这是一个 gif 动图,展示了它的效果:
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/
我是一名优秀的程序员,十分优秀!