Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get frame of a button aspect to the view?

I am creating a app and need to get the button's frame respect to the view.

I have a mapview with annotation. While selecting the annotation it shows a callout bubble. That has a button as right accessary view. I have a selector that gets invoked when I tap on that button.

Now I want to get the frame of the button with respect to the view's bound. I want to know where the button is located in the view?

For that I tried:

[self.view convertRect:btn.frame fromView:self.view];

and

[self.view convertRect:btn.frame toView:self.view];

I am not getting correct result.

like image 470
Kapil Choubisa Avatar asked Dec 19 '25 15:12

Kapil Choubisa


1 Answers

The btn.frame is relative to its (the button's) super view so passing self.view for fromView won't give the expected result because self.view is not the button's super view (in the case of an annotation callout button). So that code should be:

CGRect rect = [self.view convertRect:btn.frame fromView:btn.superview];


Similarly in the toView call, the method needs to be called on the button's super view since btn.frame is relative to it (not self.view):

CGRect rect = [btn.superview convertRect:btn.frame toView:self.view];