NullHypothesis
NullHypothesis

Reputation: 4516

Finding a CGRect in a complex view hierarchy

I am using a coachmarks/wizard framework called WSCoachMarksView that darkens your entire screen, and lets you create a highlighted area within a given CGRect to illuminate a call to action. I have a somewhat complicated hierarchy of views, and was having trouble properly identifying the CGRect I want to highlight with respect to the parent windows.

Here is my hierarchy. The goal is to find the CGRect for Day1, which is an imageview of about 50 pixels in height, and 25 pixels in width.

-TabViewController
  -NavigationViewController
     -HomeViewController : ViewController
       -Container View (container within homeviewcontroller)
====================Everything below is within this container==============
         - HomeContainerViewController : ViewController (this view is loaded within the container)
           -WeekViewController: ViewController
              DayView (just a view I dragged onto WeekViewController to hold subviews)
                Day1 (this is a rectangle imageview within the above DayView)

So there is a containerview within HomeViewController. Based on the above hierarchy, I've tried a multitude of permutations to correctly highlight Day1, but all have failed. I think my understanding of convertPoint is wrong and wanted to ask for some help in understanding which parameters to pass.

I read somewhere that you can only work off of the relationship between the parent/child but not a grandparent, etc?

Given this:

 //assume tabViewController is the TabViewController above
 //assume weekViewController is self.view, or WeekViewController

 let day1Point = weekViewController.view.convertPoint(day1.frame.origin, fromView: tabViewController.view)

So unfortunately something like this just highlights the area on the top-left of the screen (my day 1 is actually near the middle of the screen).

What's the approach for convertPoint or convertRect? Is it always the leaf node in the hierarchy tree, and the "fromView" parameter is the top-most view (parent node)?

Thanks so much, any clarification / suggestions would be very much appreciated!

edit so something like this gets me closer:

var myFrame: CGRect = dayView.convertRect(day1.frame, toView: self.view)

but it's still off slightly depending upon iPhone 5, 5s, 6, 6plus (only part of the image appears).

Upvotes: 0

Views: 126

Answers (1)

Fogmeister
Fogmeister

Reputation: 77661

Note by OP:

Fogmeister suggested in the comments to use viewWillAppear, but i'm sure he meant viewDidAppear (which is actually what led me to solve this problem). other than that, everything he suggested was spot on (i.e. the correct usage of convertPoint/Rect below). Thanks so much for your help!

Well, the way convert works is that it will take the absolute point and convert it to different reference views.

So you should be able to do something like this...

From the WeekViewController you should be able to do...

self.view.convertPoint(day1.frame.origin, toView:homeViewController.view)

or...

self.view.convertRect(day1.frame, toView:homeViewController.view)

You will need a reference to the homeViewController to do this. I',m not certain what you're trying to do though. Where is your code that is highlighting the view? Is that code in homeViewController?

Upvotes: 2

Related Questions