lukas
lukas

Reputation: 2410

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

Upvotes: 6

Views: 11664

Answers (6)

Sunny
Sunny

Reputation: 831

It can be done by using this code.

In Swift

 if UIDevice.current.userInterfaceIdiom == .pad {
    let item = self.navigationItem.rightBarButtonItem;
    let view = item?.value(forKey: "view")
    let popPresenter = alert.popoverPresentationController
    popPresenter?.sourceView = view
    popPresenter?.sourceRect = view.bounds
 }
 present(alert, animated: true, completion: nil)

In Objective-C

if([[UIDevice currentDevice]userInterfaceIdiom]== UIUserInterfaceIdiomPad) {
    UIBarButtonItem *item = self.navigationItem.rightBarButtonItem;
    UIView *view = [item valueForKey:@"view"];
    UIPopoverPresentationController *popover = alert.popoverPresentationController;
    if(view){
       popover.sourceRect = view.frame;
       popover.sourceView = view;
    }
 }

Upvotes: 0

luke8800gts
luke8800gts

Reputation: 428

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

Upvotes: 4

A.Gao
A.Gao

Reputation: 151

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)

Upvotes: 15

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];



}

Upvotes: 7

Mahesh
Mahesh

Reputation: 524

Try this in the selector method of the info button :

[self.popover presentPopoverFromRect:[(UIButton *)sender frame]
                                         inView:self.view
                       permittedArrowDirections:UIPopoverArrowDirectionUp
                                       animated:YES];

Hope this helps you!

Upvotes: 0

Infinity James
Infinity James

Reputation: 4735

My recommendation would be to use the method:

presentPopoverFromRect:inView:permittedArrowDirections:animated:

and give it an appropriate CGRect to make the UIPopoverController appear where you would like.

You could then specify exactly where you'd like the popover to originate and point to. This is not an ideal situation, but when it come to giving the popover controller a UIBarButtonItem, you have no say as to how it should use it to position itself.

Upvotes: 0

Related Questions