Search code examples
iosuiviewuiview-hierarchy

iOS: For implementing instruction overlay: need one branch of view hierarchy to be laid out before another branch is laid out


I'm trying to implement an overlay to provide instructions to the user about the other views in an app. Here's a sketch of the view hierarchy:

SomeFullScreenContainerView
    FullScreenContainerOfInstructionView
        InstructionView
    FullScreenView
        MaybeSomeIntermediateViews
            ViewToProvideInstructionsFor

I need to position InstructionView relative to ViewToProvideInstructionsFor, but to do that, I need to be sure that the branch of the view hierarchy containing ViewToProvideInstructionsFor has been laid out so that I can refer to the frame of ViewToProvideInstructionsFor to set the frame of InstructionView. How can I do this?


Solution

  • updated

    A few ideas:

    1. Use Auto Layout constraints and let the layout engine handle it.
    2. Insert FullScreenContainerOfInstructionView into the view hierarchy after FullScreenView is completed.
    3. Make [FullScreenContainerOfInstructionView layoutSubviews] a no-op until you set some flag indicating that FullScreenView is ready.
    4. Use setNeedsLayout after FullScreenView is ready and don't worry about the first pass being incorrect.

    original

    [viewThatNeedsToBeLaidOut layoutIfNeeded];