iOS Present Popover from bar button item bottom
Asked Answered
P

6

6

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

Palette answered 17/6, 2014 at 10:49 Comment(0)
D
7

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



}
Dubitable answered 17/6, 2014 at 11:30 Comment(1)
How would you get the frame of a UIBarButtonItem??Casework
I
16

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)
Ivyiwis answered 16/11, 2015 at 8:10 Comment(3)
Hey, welcome to Stack Overflow! Don't forget to format your code snippets using a 4-space indent, to make it look nicer.Knur
@StudentT Sorry that's a UIAlertController. Answer edited.Ivyiwis
As of this writing, note that the barButtonItem property of a UIPopoverPresentationController is deprecated in iOS 16. You should switch to sourceItem if possible. developer.apple.com/documentation/uikit/…Gman
D
7

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



}
Dubitable answered 17/6, 2014 at 11:30 Comment(1)
How would you get the frame of a UIBarButtonItem??Casework
J
4

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
Jeth answered 20/9, 2017 at 15:12 Comment(0)
S
0

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.

Sortilege answered 17/6, 2014 at 11:9 Comment(0)
E
0

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!

Embassy answered 17/6, 2014 at 11:31 Comment(1)
It's UIBarButtonItem, not UIButtonQuadriga
P
0

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;
    }
 }
Planter answered 25/1, 2018 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.