Rahul
Rahul

Reputation: 5844

How to get frame of UIView Nested in UIView's - ObjectiveC

I have an Application in Which there is a lot of UIView's Nested together.

I want to find the frame of some UIView's with respect to Controller.

I have setup an example:

enter image description here

I am able to find to frame of White UIView w.r.t to Controller but how can I find the frame of RedView wrt to Controller.

Code to find the Frame of White View:

      - (void)viewDidAppear:(BOOL)animated
      {
           NSLog(@"--OuterFrame---%@",NSStringFromCGRect(self.grayView.frame));
           NSLog(@"--InnerFrame---%@",NSStringFromCGRect(self.whiteView.frame));

           NSLog(@"---Frame With resepect to self.view --%@",NSStringFromCGRect([self.grayView convertRect:self.whiteView.frame toView:self.view]));
          }

And What if Their is another view inside red View.

Upvotes: 1

Views: 667

Answers (1)

Kurt Revis
Kurt Revis

Reputation: 27994

You have the right general idea. But it's easier if you use the inner view's bounds, and use self.view to convert:

// assumes innerView is a view somewhere underneath self.view
CGRect innerViewRectRelativeToController = 
    [self.view convertRect:innerView.bounds fromView:innerView];

This way you only need to specify the view to convert from, and the view to convert to. You don't need to think about what views are in between the two.

Upvotes: 3

Related Questions