开发者

convertPoint:toView: Doesn't seem to be working

开发者 https://www.devze.com 2022-12-23 03:22 出处:网络
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.

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 开发者_如何学Goof 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消