Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPopOverController pointing to an object inside a UITableViewCell?

I have a UIButton inside a UITableViewCell. When the user touches a cell's button, I want to display a UIPopoverController pointing to the button.

At the moment I have the following code in the cell's delegate (the delegate is the view controller that owns the UITableView):

-(void) onStatusButtonTouched:(UIButton *)aButton{
    StatusPickerController *statusPicker = [[StatusPickerController alloc] init];

    _popOver = nil;
    _popOver = [[UIPopoverController alloc] initWithContentViewController:statusPicker];

    [_popOver setDelegate:self];

    [_popOver presentPopoverFromRect:aButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
    [_popOver setPopoverContentSize:CGSizeMake(100, 352)];   
}

With this code the pop over is always pointing at the top of the view, because the button's y position is relative to its cell.

My question is, how can I know the button absolute position in order to point to it? Is there any method to work it out?

like image 710
Xavi Gil Avatar asked Dec 28 '22 08:12

Xavi Gil


2 Answers

Instead of the following line

[_popOver presentPopoverFromRect:aButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

try the following line

[_popOver presentPopoverFromRect:aButton.frame inView:[aButton superview] permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
like image 184
Ilanchezhian Avatar answered May 22 '23 10:05

Ilanchezhian


To expand a little on the answer by Aadhira: the button's frame is in the button's superview's coordinate system. If you want to put it in self.view, you're gonna have to convert it to the coordinate system of self.view. This can be done using the UIView method convertRect:(CGRect) toView:(UIView *), like this:

CGRect presentFromRect = [self.view convertRect:aButton.frame fromView:aButton.superview];
[_popOver presentPopoverFromRect:presentFromRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

Because this is a very common situation when using popovers, Apple provided a shortcut. This is the answer Aadhira gave.

like image 42
Daan van Hasselt Avatar answered May 22 '23 10:05

Daan van Hasselt