0

I've already found some kind of solution to the question I answered but it doesn't fix the problem I have:

(where I searched : Get position of UIView in respect to its superview's superview,

How to get the frame of a view inside another view?,

iOS - Get location of a view in a window? ...and more)

I wrote that code:

CGRect targetFrame = [self.view convertRect:self.buttonImageView.frame
                                           fromView:self.view];
CGRect superView1Frame = [self.view convertRect:self.buttonImageView.superview.frame
                                           fromView:self.view];
CGRect superView2Frame = [self.view convertRect:self.buttonImageView.superview.superview.frame
                                           fromView:self.view];
        
targetFrame.origin.y = targetFrame.origin.y + superView1Frame.origin.y + superView2Frame.origin.y;
targetFrame.origin.x = targetFrame.origin.x + superView1Frame.origin.x + superView2Frame.origin.x;

which works well and I can get the target frame I want.

But, the fact is that code is very ugly and I'm sure there is another way to do it. I've already tried that kind of things :

CGRect targetFrame = [self.wallButtonImage.superview.superview convertRect:self.wallButtonImage.frame
                                                                          fromView:self.buttonImageView.superview.superview];
return [self.wallButtonImage.superview.superview convertRect:targetFrame
                                                            fromView:self.buttonImageView.superview.superview];

But it doesn't work at all...

Question(s) : Is there a way to get the same result as I did have in my first example but using another method? Or is there simply better way to do it?

Also, I can't understand why I can't find any other solution for it, so can you please explain me what you would have done?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

The good fact is that I found the solution :

CGRect targetFrame = [self.view convertRect:self.projectTitle.bounds fromView:self.projectTitle];

I'll explain a bit for people who didn't understand like me :

  • [self.view is the view you want to get the frame's coordinates in.

  • convertRect:self.projectTitle.bounds this is for the bounds of the view you want to get the frame's coordinates.

  • fromView:self.projectTitle]; is finally the view you want to get the frame's coordinates.

  • so targerFrame is the frame of your initial view in the window's coordinates reference.

I did missunderstodd the third param, that is the reason why I was wrong !