Swift 在iOS 14的半屏幕上显示UIViewController

Swift 在iOS 14的半屏幕上显示UIViewController,swift,iphone,tvos,ios14,tvos14,Swift,Iphone,Tvos,Ios14,Tvos14,我一直在使用以下代码片段来实现仅在屏幕的一半上显示视图控制器的效果: func showPlayerView() { let controller = storyboard!.instantiateViewController(withIdentifier: "playerViewController") as! PlayerViewController controller.player = self.player controller.provid

我一直在使用以下代码片段来实现仅在屏幕的一半上显示视图控制器的效果:

func showPlayerView() {
    let controller = storyboard!.instantiateViewController(withIdentifier: "playerViewController") as! PlayerViewController
    controller.player = self.player
    controller.providesPresentationContextTransitionStyle = true
    controller.definesPresentationContext = true
    controller.transitioningDelegate = self
    
    self.present(controller, animated: true, completion: nil)
}

extension ViewController : UIViewControllerTransitioningDelegate
{
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        if presented is PlayerViewController {
            return HalfSizePresentationController(presentedViewController: presented, presenting: presenting)
        }
        return nil
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return animator
    }
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return animator
    }
}

class HalfSizePresentationController : UIPresentationController {
    override var frameOfPresentedViewInContainerView: CGRect
    {
        get {
            let height: CGFloat = 200
            return CGRect(x: 0, y: UIScreen.main.bounds.height - height, width: UIScreen.main.bounds.width, height: 200)
        }
    }
}
这曾经在tvOS 13上起作用,但自从tvOS 14之后,我得到的是:


你知道如何在iOS 14上实现这一点吗?

显然,问题在于tvOS 11将默认转换样式设置为与以前不同的样式

我可以通过添加以下行来解决此问题:

controller.modalPresentationStyle = .custom
就在调用self.present(控制器,动画:true,完成:nil)之前

但是,这引入了一个新问题,导致原始ViewController在动画结束后消失,如下所述:

通过将以下行添加到my
UIPresentationController
,最终解决了此问题:

override func dismissalTransitionDidEnd(_ completed: Bool) {
    if let window = UIApplication.shared.keyWindow {
        if let viewController = window.rootViewController {
            window.addSubview(viewController.view)
        }
    }
}