我试图从 TableViewController 内的按钮呈现一个 ViewController(嵌入在 NavigationController 中)。呈现的 ViewController 应该是 TableViewController 高度的一半。我尝试过下面的代码,但它似乎不起作用(Swift 3)。有人可以帮忙吗?谢谢!
class AddNewRecipeTableViewController: UITableViewController, UIViewControllerTransitioningDelegate {
@IBAction func popUpTest(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let pvc = storyboard.instantiateViewController(withIdentifier: "popUpTest") as! UINavigationController
pvc.modalPresentationStyle = UIModalPresentationStyle.custom
pvc.transitioningDelegate = self
self.present(pvc, animated: true, completion: nil)
}
func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController!, sourceViewController source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presenting: presentingViewController)
}
}
class HalfSizePresentationController : UIPresentationController {
override var frameOfPresentedViewInContainerView : CGRect {
return CGRect(x: 0, y: 0, width: containerView!.bounds.width, height: containerView!.bounds.height/2)
}
}
最佳答案
你有:
func presentationControllerForPresentedViewController(
presented: UIViewController,
presentingViewController presenting: UIViewController!,
sourceViewController source: UIViewController)
-> UIPresentationController? {
该方法永远不会被调用,因为在 Swift 3 中它不对应于 Cocoa 知道的任何方法。 (我很惊讶你没有报告从编译器收到关于此的警告。)
您可能打算实现 presentationController(forPresented:presenting:source:)
,如下所示:
func presentationController(
forPresented presented: UIViewController,
presenting: UIViewController?,
source: UIViewController)
-> UIPresentationController? {
但即使这样也不会被调用,因为您尚未将呈现的 View Controller 的 modalPresentationStyle
设置为 .custom
。
关于ios - 当前模态未正确调整 View 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42230805/