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?
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With