【问题标题】:Presenting UIViewController on half of screen on iOS 14在 iOS 14 上的一半屏幕上呈现 UIViewController
【发布时间】:2020-11-18 10:17:55
【问题描述】:

我一直在使用下面的代码sn-p来实现只在一半屏幕上呈现视图控制器的效果:

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 上实现这一点吗?

【问题讨论】:

    标签: swift iphone tvos ios14 tvos14


    【解决方案1】:

    显然问题在于 tvOS 11 将默认过渡样式设置为与以前不同的样式。

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

    controller.modalPresentationStyle = .custom
    

    就在致电self.present(controller, animated: true, completion: nil)之前。

    但是,这引入了一个新问题,导致原始 ViewController 在动画结束后消失,如下所述:Presented view controller disappears after animation using custom UIViewController animations

    最终通过在我的UIPresentationController 中添加以下几行来解决它:

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

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2018-05-06
      相关资源
      最近更新 更多