【问题标题】:popover is not working in swift 4popover 在 swift 4 中不起作用
【发布时间】:2018-06-29 05:43:55
【问题描述】:

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

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)
}

【问题讨论】:

  • 适用于 iPad 还是 iPhone?
  • 适用于 iPhone

标签: ios swift uipopoverpresentationcontroller


【解决方案1】:

不要使用preferredContentSize,而是尝试使用UIPopoverPresentationControllersourceRect 属性,

showObjectVC.popoverPresentationController?.sourceRect = tableView.cellForRow(at: indexPath)!.frame

确保根据需要指向单元格框架,以便弹出框显示在您想要的位置。

【讨论】:

    【解决方案2】:

    您可以尝试另一种方法:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多