Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPad popover presentpopoverfrombarbuttonitem

Tags:

ios

ipad

popover

I have added a few buttons to the right side of the navigation bar with the following:

UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
customView.backgroundColor = [UIColor clearColor];

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,  0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:@"toc.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:@selector(tableOfContentsAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];

button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 0, 45, 44);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.backgroundColor = [UIColor clearColor];
[button setImage:[UIImage imageNamed:@"bookmark.png"] forState:UIControlStateNormal];
button.userInteractionEnabled = YES;
[button addTarget:self action:@selector(bookmarkButtonAction) forControlEvents:UIControlEventTouchUpInside];
[customView addSubview:button];

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:customView];

self.navigationItem.rightBarButtonItem = segmentBarItem;
[customView release];
[segmentBarItem release];

This works well. For both buttons I show a popOver as shown below

- (void) bookmarkButtonAction
{
BookmarksViewController* content = [[BookmarksViewController alloc] initWithOrientation:lastOrientation selectedPage:selectedPage];
UIPopoverController* aPopover = [[UIPopoverController alloc] initWithContentViewController:content];
CGSize size = content.view.frame.size;
aPopover.popoverContentSize = size;
aPopover.delegate = self;
self.bookmarksPopoverVC = content;
self.bookmarksPopoverVC.popUpController = aPopover;
[content release];
[aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[aPopover release];
bookmarksShowing = YES;
}

The problem is that I am using presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem and this shows the top arrow in the middle of the two buttons. How can I attach the arrow to each button?

like image 826
booboo-a-choo Avatar asked Dec 12 '22 19:12

booboo-a-choo


2 Answers

instead of using this line:

aPopover presentPopoverFromBarButtonItem:self.navigationItem.rightBarButtonItem

You may better try this line:

aPopover presentPopoverFromBarButtonItem:sender

I think that would solve your problem

like image 193
Polo Swelsen Avatar answered Dec 22 '22 00:12

Polo Swelsen


try this:

- (IBAction)products:(id)sender {
    UIButton* btn = (UIButton *)sender;
    [productsPopover presentPopoverFromRect:[btn bounds] inView:btn permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

Works like a charm

like image 29
Enlil Avatar answered Dec 22 '22 00:12

Enlil