popover无法快速运行4

时间:2018-06-29 05:43:55

标签: ios swift uipopoverpresentationcontroller

我正在使用UIPopoverPresentationControllerDelegate来显示我的弹出框,它工作正常,但无法管理弹出框viewController的宽度和高度。以及如何在当前上下文中设置Presentation?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let showObjectVC = storyboard?.instantiateViewController(withIdentifier: "ShowActorDetail") as! ShowActorDetailsViewController
    showObjectVC.modalPresentationStyle = .popover

    showObjectVC.popoverPresentationController?.sourceView = tableView.cellForRow(at: indexPath)
    showObjectVC.popoverPresentationController?.delegate = self
    showObjectVC.preferredContentSize = CGSize(width: 400, height: 300)

    present(showObjectVC, animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:0)

您可以尝试另一种方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let showObjectVC = storyboard?.instantiateViewController(withIdentifier: "ShowActorDetail") as! ShowActorDetailsViewController
    // your logic
    presentViewControllerAsPopUp(showObjectVC)
}

private func presentViewControllerAsPopUp(_ vc: UIViewController) {
    vc.modalPresentationStyle = .overCurrentContext
    vc.modalTransitionStyle = .crossDissolve
    //self.definesPresentationContext = true
    self.present(vc, animated: true, completion: nil)
}

ShowActorDetailsViewController中,您可以制作UIView并具有所需的大小和彩色背景。

答案 1 :(得分:0)

请不要像使用preferredContentSize那样使用sourceRect的{​​{1}}属性,

UIPopoverPresentationController

请确保根据需要指向单元格框架,以使弹出窗口显示在所需位置。