Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop over doesn't point over the button

I have an application that is compatible with both iPhone and iPad layouts. For iPhone layout I have created Action Sheet and Pop over for iPad. The problem is pop over's arrow does not point over the button I clicked. Below is my code....

let actionSheet = UIAlertController(title: "Choose an option",
            message: "Message",
            preferredStyle: .ActionSheet)
...

if UIDevice.currentDevice().userInterfaceIdiom == UIUserInterfaceIdiom.Pad
{
     // for iPad
     actionSheet.popoverPresentationController?.sourceView = self.view
     actionSheet.popoverPresentationController?.sourceRect = self.view.bounds;
     actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.allZeros;
}

self.presentViewController(actionSheet, animated: true, completion: nil)
like image 850
Srujan Simha Avatar asked Jan 07 '15 15:01

Srujan Simha


3 Answers

Set the sourceView and sourceRect as the button and button.bounds.
You can choose the permittedArrowDirections depending on the layout of your view.

actionSheet.popoverPresentationController?.sourceView = button
actionSheet.popoverPresentationController?.sourceRect = button.bounds;
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Left;

If the button is a BarButtonItem use this code.

actionSheet.popoverPresentationController?.barButtonItem = button
actionSheet.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up;
like image 196
rakeshbs Avatar answered Nov 20 '22 18:11

rakeshbs


For me worked using the sender and casting as UIView.

alertController.popoverPresentationController?.sourceView = sender as! UIView
alertController.popoverPresentationController?.sourceRect = sender.bounds

alertController.popoverPresentationController?.permittedArrowDirections = UIPopoverArrowDirection.Up
like image 43
Javier Gomez Avatar answered Nov 20 '22 19:11

Javier Gomez


SWIFT 3

This worked for me when my button was a UIBarButtonItem:

if UIDevice.current.userInterfaceIdiom == .pad {

    if controller.responds(to: "popoverPresentationController") {
        controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
    }

}

Entire code snippet below:

func presentActivitySheet() {

    let controller = UIActivityViewController(activityItems: [document.fileURL], applicationActivities: nil)

        if UIDevice.current.userInterfaceIdiom == .pad {

            if controller.responds(to: "popoverPresentationController") {
            controller.popoverPresentationController?.barButtonItem = YourUIBarButtonName
            }

        }

    present(controller, animated: true, completion: nil)
}
like image 1
A.J. Hernandez Avatar answered Nov 20 '22 19:11

A.J. Hernandez