我试图modal view controller在另一个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)
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当用户单击父视图控制器时,我需要关闭我的半角视图控制器.
怎么做到这一点?
您只需将 UIView 添加到 HalfSizePresentationController (可能是一个调光视图)。然后向该视图添加一个点击手势识别器来处理将被调用以关闭呈现视图的点击事件:
func dimmingViewTapped(tapRecognizer: UITapGestureRecognizer) {
presentingViewController.dismissViewControllerAnimated(true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
也许这会有所帮助:http://zappdesigntemplates.com/custom-presentations-using-uipresentationcontroller-swift/
| 归档时间: |
|
| 查看次数: |
3957 次 |
| 最近记录: |