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

  • SEARCH
  • Home
  • 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 8775883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:54:57+00:00 2026-06-13T18:54:57+00:00

When subclassing UIView , I usually place all my initialisation and layout code in

  • 0

When subclassing UIView, I usually place all my initialisation and layout code in its init method. But I’m told that the layout code should be done by overriding layoutSuviews. There’s a post on SO that explains when each method gets called, but I’d like to know how to use them in practice.

I currently put all my code in the init method, like this:

MyLongView.m

- (id)initWithHorizontalPlates:(int)theNumberOfPlates
{
    self = [super initWithFrame:CGRectMake(0, 0, 768, 1024)];

    if (self) {
        // Initialization code
        _numberOfPlates = theNumberOfPlates;

        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
        [scrollView setContentSize:CGSizeMake(self.bounds.size.width* _numberOfPlates, self.bounds.size.height)];
        [self addSubview:scrollView];

        for(int i = 0; i < _numberOfPlates; i++){
            UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"a1greatnorth_normal_%d.jpg", i+1]];
            UIImageView *plateImage = [[UIImageView alloc] initWithImage:img];
            [scrollView addSubview:plateImage];
            plateImage.center = CGPointMake((plateImage.bounds.size.width/2) + plateImage.bounds.size.width*i, plateImage.bounds.size.height/2);
        }

    }
    return self;
}

It’s the usual tasks: setting up the view’s frame, initialising an ivar, setting up a scrollview, initialising UIImages, placing them in UIImageViews, laying them out.

My question is: which of these should be done in init, and which of these should be done in layoutSubviews?

  • 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-06-13T18:54:59+00:00Added an answer on June 13, 2026 at 6:54 pm

    Your init should create all the objects, with the required data. Any frame you pass to them in init should ideally be their starting positions.

    Then, within layoutSubviews:, you change the frames of all your elements to place them where they should go. No alloc’ing or init’ing should take place in layoutSubviews:, only the changing of their positions, sizes etc…

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

Sidebar

Related Questions

Without subclassing a UIResponder class(e.g. UIView) and overriding its touchesBegan:withEvent: method, is it possible
I am subclassing a UIGestureRecogniser, but for some reason the method - (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)preventingGestureRecognizer{
Subclassing of UIWebView is restricted by Apple. But I need to replace method canPerformAction
I am subclassing UIView and using instances of that to set my UITableViewCell backgroundView
When subclassing an CALayer and implementing the drawInContext method, I would assume that any
Possible Duplicate: Subclassing UIView to draw in Quartz Hello i am searching good example
I am subclassing UITextView, and i got it working, but i also want to
I'm subclassing QProgressBar in a custom widget, and I overwrote the paintEvent method with
I'm subclassing UIView using the standard EAGLView framework. When I detect orientation changes, I
I'm writing a small painting app for iOS. I'm subclassing a UIView ad performing

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.