PushViewController()导致弹出框为全屏

时间:2018-09-23 22:55:53

标签: ios swift uiview popover

我想在我的视图控制器顶部放置一个弹出框。目前,此代码位于我的UIView的子类中:

func presentGameOver() {
        let transition: CATransition = CATransition()
        transition.duration = 0.75
        transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
        transition.type = CATransitionType.fade

        let currentController = (getCurrentViewController() as! UINavigationController).topViewController!
        currentController.navigationController!.view.layer.add(transition, forKey: nil)

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "GameOverViewController") as! GameOverViewController
        vc.modalPresentationStyle = UIModalPresentationStyle.popover
        vc.highScore = highScore
        vc.score = score
        vc.FONT_H = FONT_H
        vc.delegate = self

        currentController.navigationController?.pushViewController(vc, animated: false)
    }

这是我的课程声明:

class GridView: UIView, ModalHandler, UIPopoverPresentationControllerDelegate {

我也有这两种方法:

func popoverPresentationControllerShouldDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) -> Bool {
    return false
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

在情节提要中,对于我想成为弹出窗口的视图控制器,我设置了内容大小(thisthis)。

但是,当显示弹出框时,它会全屏显示。当我以前使用弹出窗口时,我会介绍它们。使用pushViewController()无法显示弹出窗口吗?

1 个答案:

答案 0 :(得分:0)

如果您没有经验,动画会非常复杂。以下代码可能会为您提供一些线索。

    func presentGameOver() {
    let transition: CATransition = CATransition()
    transition.duration = 0.75
    transition.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition.type = CATransitionType.fade

    let currentController = (getCurrentViewController() as! UINavigationController).topViewController!
   currentController.navigationController!.view.layer.add(transition, forKey: nil)

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "GameOverViewController") as! GameOverViewController
    vc.modalPresentationStyle = UIModalPresentationStyle.popover
    vc.highScore = highScore
    vc.score = score
    vc.FONT_H = FONT_H
    vc.delegate = self
    if let popoverPresentationController =  vc.popoverPresentationController{
        popoverPresentationController.delegate = self;
        popoverPresentationController.sourceView  = self
        popoverPresentationController.sourceRect  = CGRect.init(x: 200, y: 200, width: 500, height: 300)}
    currentController.present(vc, animated: false) {}

}