Поповер не работает в Swift 4

Я использую 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)
}

2 ответа

Вместо того, чтобы идти с preferredContentSize попробуйте использовать sourceRect собственностью UIPopoverPresentationController лайк,

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

Обязательно наведите рамку ячейки так, как вам нужно, чтобы всплывающее окно показывало в нужной вам позиции.

Вы можете попробовать другой подход:

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 с необходимым размером и цветным фоном.

Другие вопросы по тегам