Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convertPoint:toView: Doesn't seem to be working

I have a TableView with custom TableCellViews that has UILabels and UIButtons on it. when one of the buttons is taped I want to show a "tooltip" describing the text of the button.

Most everything is working except for when I try to convert the center coordinates of the UIButton to the coordinates of the rootView which is a UIView.

Here is the code:

- (void) fancyLabelButtonPressed: (UIButton *) button {
  CGPoint btnPoint = button.center;   // x=200.5 y=27.5
  CGPoint rootViewPoint = [button convertPoint:btnPoint toView:rootView];
  // rootViewPoint -> x=390.5 y=197.5
  CGPoint pointToUse = CGPointMake(btnPoint.x +20, rootViewPoint.y - 23); // Hack to get it close
}

How can rootViewPoint.x=390.5 when I'm in portrait view?!!? By using the x from the button and the y from the rootViewPoint I get close to what it should be but it is just a hack.

Does anyone see what I'm doing wrong? or is there a better way?

like image 225
KingAndrew Avatar asked Mar 11 '10 21:03

KingAndrew


1 Answers

This is because you are converting the point from the wrong view. The center property is actually in the coordinate system of the button's superview, whatever that is, so when you convert it to your rootView, you must convert it from there. So:

rootViewPoint = [[button superview] convertPoint:btnPoint toView:rootView];

That should give you what you are looking for.

like image 147
Jason Coco Avatar answered Nov 09 '22 12:11

Jason Coco