繁体   English   中英

在Swift中设置自定义展示模式的大小失败-占据全屏

[英]Set custom size of presenting modal in Swift fails — occupies full screen

我怎样才能使呈现模态具有自定义大小? 尝试了许多不同的解决方案,其中许多似乎已过时

这是我从父视图控制器实例化模式视图的方式:

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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM