Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Present Popover from bar button item bottom

I have an UIPopoverController that presents an UIViewController using this method:

[self.popover presentPopoverFromBarButtonItem:self.infoBarButtonItem 
                     permittedArrowDirections:UIPopoverArrowDirectionUp
                                     animated:YES];

self.popover is my UIPopoverController.

The code works well, but the Popover arrow is in the middle of the BarButtonItem how do I display the Popover with its arrow "under" the button?

This is what it currently looks like:
altText

like image 750
lukas Avatar asked Jun 17 '14 10:06

lukas


3 Answers

In iOS 9 you can try this code:

let alert = UIAlertController(title: "title", message: "message", preferredStyle: UIAlertControllerStyle.ActionSheet)
alert.modalPresentationStyle = UIModalPresentationStyle.Popover
alert.popoverPresentationController?.barButtonItem = sender
presentViewController(alert, animated: true, completion: nil)
like image 193
A.Gao Avatar answered Oct 13 '22 19:10

A.Gao


How about this

UIBarButtonItem *item = self.infoBarButtonItem ;

UIView *view = [item valueForKey:@"view"];

if(view){

        CGRect frame=view.frame;

        [self.popover presentPopoverFromRect:frame
                                      inView:view.superview
                    permittedArrowDirections:UIPopoverArrowDirectionUp
                                    animated:YES];



}
like image 7
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 13 '22 18:10

Vijay-Apple-Dev.blogspot.com


For all who are using UIViewController with PopoverPresentationController (iOS 9+); This worked for me: you can directly set your UIBarButtonItem like this.

myPopoverPresentationController?.barButtonItem = myUIBarButtonItem
like image 4
luke8800gts Avatar answered Oct 13 '22 19:10

luke8800gts