如何成长一个弹出控制器

How to grow a popover controller

我的应用程序中有一个用于 iPad 和 Mac 的弹出窗口控制器(使用 mac 催化剂),我正在尝试找出如何增加弹出窗口的高度当它已经呈现时。我一直在到处搜索如何执行此操作,但我发现的所有内容都是关于在呈现之前而不是之后设置大小。

弹出窗口显示时,其中有一个按钮应该将高度增加 100-150 像素,但我不知道如何

谁能帮我解决这个问题?提前致谢!

这是我的弹出窗口呈现代码:

func openView(sourceView: UIView?, sourceRect: CGRect?) {
    let vc = ViewControllerA()

    //Try popover
    if let sourceView = sourceView,
        let sourceRect = sourceRect {
            vc.modalPresentationStyle = .popover
            
            if let popover = vc.popoverPresentationController {
                vc.preferredContentSize = return CGSize(width: 390, height: 390)
                vc.presentationController?.delegate = self

                popover.sourceView = sourceView
                popover.sourceRect = sourceRect
                popover.permittedArrowDirections = [.up, .left]

                self.present(vc, animated: true, completion: nil)
                return
            }
        }


    //present it normally if there's no source view
    vc.modalPresentationStyle = .overCurrentContext
    vc.modalTransitionStyle = .crossDissolve

    self.present(vc, animated: true, completion: nil)
}

要更改弹出视图中显示的控制器的大小,您应该修改其 preferredContentSize 属性:

@IBAction func showPopover(_ sender: UIButton) {
    // Create controller
    guard let controller = storyboard?.instantiateViewController(withIdentifier: "Popover") else {
        return
    }
    controller.modalPresentationStyle = .popover
    controller.preferredContentSize = CGSize(width: 200, height: 200)
    
    // Configure popover
    guard let popover = controller.popoverPresentationController else {
        return
    }
    popover.permittedArrowDirections = .up
    popover.sourceView = self.view
    popover.sourceRect = sender.frame
    popover.delegate = self
    present(controller, animated: true, completion: nil)
    
    // Change height after 3 secs
    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
        controller.preferredContentSize = CGSize(width: 200, height: 400)
    }
}