以一半的大小呈现模态视图控制器

6

我尝试实现将模态视图控制器呈现在另一个UIViewController上,大小为父视图控制器的一半:

class ViewController: UIViewController, UIViewControllerTransitioningDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func tap(sender: AnyObject) {
        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        var pvc = storyboard.instantiateViewControllerWithIdentifier("CustomTableViewController") as UITableViewController

        pvc.modalPresentationStyle = UIModalPresentationStyle.Custom
        pvc.transitioningDelegate = self
        pvc.view.backgroundColor = UIColor.redColor()

        self.presentViewController(pvc, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presenting)
    }
}

class HalfSizePresentationController : UIPresentationController {
    override func frameOfPresentedViewInContainerView() -> CGRect {
        return CGRect(x: 0, y: 0, width: containerView.bounds.width, height: containerView.bounds.height/2)
    }
}

现在,当用户点击父视图控制器时,我需要关闭我的半尺寸视图控制器。如何实现这一点?

为什么我尝试按照你的方法显示半尺寸弹出式视图控制器,但没有起作用? - TomSawyer
3个回答

3
以下内容是可行的:(在运行Swift 3,iOS 10.2的Xcode 8.3中进行了检查)
class HalfSizePresentationController: UIPresentationController {

let backgroundView = UIView()

override var frameOfPresentedViewInContainerView: CGRect {
    // your implementation
}

override func presentationTransitionDidEnd(_ completed: Bool) {
    if completed {
        backgroundView.frame = (containerView?.frame)!
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(AddMenuPresentationController.dismissPVC(_:)))
        backgroundView.addGestureRecognizer(tapGestureRecognizer)
        containerView?.insertSubview(backgroundView, at: 0)
    }
}

override func dismissalTransitionDidEnd(_ completed: Bool) {
    if completed {
        backgroundView.removeFromSuperview()
    }
}

func dismissPVC(_ gestureRecognizer: UIGestureRecognizer) {
    self.presentedViewController.dismiss(animated: true, completion: nil)
}
}

确保在索引0处插入视图。

1

我知道现在已经太晚了。但是下面是实现这个目标的最简单答案。

self.view.backgroundColor = UIColor.clearColor()
self.datePicker.backgroundColor = UIColor.whiteColor()
self.modalTransitionStyle = .CoverVertical
self.modalPresentationStyle = .OverCurrentContext

其中 self 是被呈现的视图控制器。

如果我理解正确的话,我尝试过这个,但是.overCurrentContext会破坏整个自定义呈现,就像它对我所做的那样。 - Natanel

0
你只需要向HalfSizePresentationController中添加一个UIView(可以是遮罩视图)。然后向该视图添加一个点击手势识别器,以处理调用以关闭呈现视图的触摸事件。
func dimmingViewTapped(tapRecognizer: UITapGestureRecognizer) {
        presentingViewController.dismissViewControllerAnimated(true, completion: nil)
    }

也许这会有所帮助:http://zappdesigntemplates.com/custom-presentations-using-uipresentationcontroller-swift/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接