Display pop-ups from the Bar button in the navigation bar on iPhone

In Swift, I am trying to show a popover from a panel button element located in the upper right position of the navigation bar. Below is my code:

func showOptions(sender: UIBarButtonItem) {
    let optionsVC = OptionsViewController(nibName: "OptionsViewController", bundle: nil)
    optionsVC.delegate = self
    optionsVC.modalPresentationStyle = .popover
    optionsVC.preferredContentSize = CGSize(width: 200, height: 200)

    present(optionsVC, animated: true, completion: nil)

    let popController = optionsVC.popoverPresentationController
    popController?.permittedArrowDirections = .up
    popController?.delegate = self
    popController?.barButtonItem = sender
}

func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
    return .none
}

His work is good on the iPad, not on the iPhone. I looked through the documentation and various web pages. Everything seems to be correct. What is missing in my code?

+4
source share
1 answer

The only problem is what you imagine OptionsViewControllerbefore installing your delegate popover. Therefore, first set its delegate, then call the current function.

let popController = optionsVC.popoverPresentationController
popController?.permittedArrowDirections = .up
popController?.delegate = self
popController?.barButtonItem = sender

present(optionsVC, animated: true, completion: nil)
+6
source

Source: https://habr.com/ru/post/1664963/


All Articles