我试图在另一个大小为一半父 View Controller 的 UIViewController
上实现呈现 modal view controller
为:
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)
}
}
现在我需要在用户单击父 View Controller 时关闭我的半尺寸 View Controller 。
如何实现?
最佳答案
以下作品:(在运行 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处插入 View
关于ios - 以半尺寸显示模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31288017/