Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 853493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:46:05+00:00 2026-05-15T07:46:05+00:00

Can anyone give a definitive explanation on the relationship between UIView’s setNeedsLayout , layoutIfNeeded

  • 0

Can anyone give a definitive explanation on the relationship between UIView's setNeedsLayout, layoutIfNeeded and layoutSubviews methods? And an example implementation where all three would be used. Thanks.

What gets me confused is that if I send my custom view a setNeedsLayout message the very next thing it invokes after this method is layoutSubviews, skipping right over layoutIfNeeded. From the docs I would expect the flow to be setNeedsLayout > causes layoutIfNeeded to be called > causes layoutSubviews to be called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:46:06+00:00Added an answer on May 15, 2026 at 7:46 am

    I’m still trying to figure this out myself, so take this with some skepticism and forgive me if it contains errors.

    setNeedsLayout is an easy one: it just sets a flag somewhere in the UIView that marks it as needing layout. That will force layoutSubviews to be called on the view before the next redraw happens. Note that in many cases you don’t need to call this explicitly, because of the autoresizesSubviews property. If that’s set (which it is by default) then any change to a view’s frame will cause the view to lay out its subviews.

    layoutSubviews is the method in which you do all the interesting stuff. It’s the equivalent of drawRect for layout, if you will. A trivial example might be:

    -(void)layoutSubviews {
        // Child's frame is always equal to our bounds inset by 8px
        self.subview1.frame = CGRectInset(self.bounds, 8.0, 8.0);
        // It seems likely that this is incorrect:
        // [self.subview1 layoutSubviews];
        // ... and this is correct:
        [self.subview1 setNeedsLayout];
        // but I don't claim to know definitively.
    }
    

    AFAIK layoutIfNeeded isn’t generally meant to be overridden in your subclass. It’s a method that you’re meant to call when you want a view to be laid out right now. Apple’s implementation might look something like this:

    -(void)layoutIfNeeded {
        if (self._needsLayout) {
            UIView *sv = self.superview;
            if (sv._needsLayout) {
                [sv layoutIfNeeded];
            } else {
                [self layoutSubviews];
            }
        }
    }
    

    You would call layoutIfNeeded on a view to force it (and its superviews as necessary) to be laid out immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.