问题描述
我正在尝试在大小为父视图控制器一半的其他视图控制器上呈现模态视图控制器.但它始终以全屏视图显示.
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
以用于半尺寸演示:
For this you let the presenting ViewController
implement the UIViewControllerTransitioningDelegate
and return your PresentationController
for the half sized presentation:
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presenting: presentingViewController)
}
演示时,您将演示样式设置为 .Custom
并设置您的过渡代理:
When presenting you set the presentation style to .Custom
and set your transitioning delegate:
pvc.modalPresentationStyle = .custom
pvc.transitioningDelegate = self
呈现控制器只返回呈现的视图控制器的框架:
The presentation controller only returns the frame for your presented view controller:
class HalfSizePresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
guard let bounds = containerView?.bounds else { return .zero }
return CGRect(x: 0, y: bounds.height / 2, width: bounds.width, height: bounds.height / 2)
}
}
这里是完整的工作代码:
Here is the working code in its entirety:
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
@IBAction func tap(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let pvc = storyboard.instantiateViewController(withIdentifier: "CustomTableViewController") as! UITableViewController
pvc.modalPresentationStyle = .custom
pvc.transitioningDelegate = self
pvc.view.backgroundColor = .red
present(pvc, animated: true)
}
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presenting: presentingViewController)
}
}
class HalfSizePresentationController: UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
guard let bounds = containerView?.bounds else { return .zero }
return CGRect(x: 0, y: bounds.height / 2, width: bounds.width, height: bounds.height / 2)
}
}
这篇关于在半尺寸父控制器中显示模态视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!