Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing pop over from Bar Button in Navigation bar in iPhone

In Swift, I'm trying to show a popover from a bar button item present in the top right position of 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
}

Its working good in iPad and not in iPhone. I've gone through the documentation and different web pages. Everything seems to be right. What's missing in my code?

like image 382
Satyam Avatar asked Dec 27 '16 05:12

Satyam


People also ask

What is a popover ios?

A popover is a transient view that appears above other content onscreen when you tap a control or in an area. Typically, a popover includes an arrow pointing to the location from which it emerged.

How do I present Viewcontroller in popover Swift?

In the Bar Button Item section Change the System Item to Action. Next, Drag a new View Controller from the Object Library on to the Storyboard. Drag a Text View from the Object Library at the top of this View Controller. This View Controller will be displayed in the popover.


1 Answers

The only problem here is you are presenting OptionsViewController before setting its popover delegate. So first set its delegate then call present function.

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

present(optionsVC, animated: true, completion: nil)
like image 121
Hamza Ansari Avatar answered Oct 23 '22 15:10

Hamza Ansari