ios - 当前模态未正确调整 View 大小

标签 ios swift

我试图从 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/

相关文章:

swift - 无法将 Observable<Store> 类型的类型值转换为预期参数类型 Observable<Store?>

ios - 多个 segues 到同一个 View Controller

ios - 为 FCM 检索 token 的正确方法 - iOS 10 Swift 3

ios - Swift 覆盖子类中的协议(protocol)方法

iOS Swift - 计算两次之间的差异

ios - 如何在 iOS 中为 AvcaptureSession 设置 videoMaximumDuration

ios - 如何在 Swift 中从当年的每个月中获取一半

ios - 内存中的 AudioKit/DSP : Understanding the different between the size of uncompressed audio on disk vs.

ios - 选择后永久更改tableView中的背景单元格

ios - Swift:将 UIImage 设置为背景