如何使演示模型具有自定义大小?尝试了很多不同的解决方案,其中很多看起来已经过时了
这就是我如何从父视图控制器实例化模式视图:

self.definesPresentationContext = true
let vc = (storyboard?.instantiateViewController(withIdentifier: "modalViewController"))!
vc.modalPresentationStyle = .overCurrentContext
vc.preferredContentSize = CGSize(width: 100, height: 100)
present(vc, animated: true, completion: nil)

但是,模态视图覆盖了整个屏幕,而不是仅仅占用100*100。

最佳答案

您需要实现UIViewControllerTransitioningDelegate方法和UIViewControllerAnimatedTransitioning方法来定制呈现的UIViewController大小。
要知道如何实现自定义动画,
参考:https://github.com/pgpt10/Custom-Animator
编辑:

class ViewController: UIViewController
{
    //MARK: Private Properties
    fileprivate let animator = Animator()

    //MARK: View Lifecycle Methods
    override func viewDidLoad()
    {
        super.viewDidLoad()
    }

    override func awakeFromNib()
    {
        super.awakeFromNib()
        self.transitioningDelegate = self
        self.modalPresentationStyle = .custom
    }

    //MARK: Button Action Methods
    @IBAction func dismissController(_ sender: UIButton)
    {
        self.dismiss(animated: true, completion: nil)
    }
}

// MARK: - UIViewControllerTransitioningDelegate Methods
extension ViewController : UIViewControllerTransitioningDelegate
{
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        self.animator.transitionType = .zoom
        self.animator.size = CGSize(width: 100, height: 100)
        return self.animator
    }

    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        return self.animator
    }
}

关于ios - 在Swift中设置自定义展示模式的大小失败-占据全屏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44302590/

10-09 16:14