Does a UIPopOverController point to an object inside a UITableViewCell?

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

Currently, I have the following code in the cell 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, a tooltip always points to the top of the view, since the position of the y button is relative to its cell.

My question is: how can I find out the absolute position of a button to point to it? Is there any way to solve it?

+4
source share
2 answers

Instead of the next line

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

try the next line

 [_popOver presentPopoverFromRect:aButton.frame inView:[aButton superview] permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES]; 
+8
source

To slightly expand the answer from Aadhira: the button frame is in the viewView coordinate system. If you want to put it in self.view, you will have to convert it to a coordinate system self.view. This can be done using the UIView method convertRect convertRect:(CGRect) toView:(UIView *) , for example:

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

Since this is a very common situation when using popovers, Apple provided a shortcut. This is the answer that Aadhir gave.

+2
source

Source: https://habr.com/ru/post/1388586/


All Articles