在半尺寸父控制器中呈现模态视图控制器 [英] Present modal view controller in half size parent controller
问题描述
我试图在其他viewController上呈现模态视图控制器,其大小为半父视图控制器。但它始终以全屏视图显示。
I am trying to present modal view controller on other viewcontroller sized to half parent view controller. But it always present in full screen view.
我在我的故事板中创建了具有固定帧大小的自由形式视图控制器。 320 X 250。
I have created freeform sized View controller in my storyboard with fixed frame size. 320 X 250.
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var pvc = storyboard.instantiateViewControllerWithIdentifier("CustomTableViewController") as ProductsTableViewController
self.presentViewController(pvc, animated: true, completion: nil)
我试图设置frame.superview但它没有帮助。
I have tried to set frame.superview and it doesn't help.
请提供建议。
推荐答案
您可以使用 UIPresentationController
来实现这一目标。
You can use a UIPresentationController
to achieve this.
为此你要让提交 ViewController
实现 UIViewControllerTransitioningDelegate
并返回 PresentationController
half size presentation:
For this you let the presenting ViewController
implement the UIViewControllerTransitioningDelegate
and return your PresentationController
for the half sized presentation:
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presenting)
}
在演示时,您将演示文稿样式设置为 .Custom
并设置转换代理:
When presenting you set the presentation style to .Custom
and set your transitioning delegate:
pvc.modalPresentationStyle = UIModalPresentationStyle.Custom
pvc.transitioningDelegate = self
演示文稿controller仅返回您呈现的视图控制器的帧:
The presentation controller only returns the frame for your presented view controller:
class HalfSizePresentationController : UIPresentationController {
override func frameOfPresentedViewInContainerView() -> CGRect {
return CGRect(x: 0, y: 0, width: containerView.bounds.width, height: containerView.bounds.height/2)
}
}
以下是完整的工作代码:
Here is the working code in its entirety:
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
@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)
}
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presentingViewController: presentingViewController)
}
}
class HalfSizePresentationController : UIPresentationController {
override func frameOfPresentedViewInContainerView() -> CGRect {
return CGRect(x: 0, y: 0, width: containerView.bounds.width, height: containerView.bounds.height/2)
}
}
这篇关于在半尺寸父控制器中呈现模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!