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 3286366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:22:18+00:00 2026-05-17T20:22:18+00:00

I currently have a UIView subclass that acts as my header view for my

  • 0

I currently have a UIView subclass that acts as my header view for my UITableViewController. All of the subviews vary in size depending on data retrieved for a particular item.

layoutSubViews is getting called for the UIView before I can determine the size of each label. This causes a problem because I set the size of the view within the layoutSubViews method. Since it gets called before I setup my labels, the views height is always 0. Even after setting up the labels I call setNeedsLayout but the table views header size does not change.

This will create my TableHeaderView and set the text for my labels.

    TableHeaderView *tableHeaderView = [[TableHeaderView alloc] initWithFrame:CGRectZero];
    tableHeaderView.headerTitle.text = title;
    tableHeaderView.headerOption1.text = headerOption1
    tableHeaderView.headerOption2.text = headerOption2
    tableHeaderView.headerOption3.text = headerOption3

    [[self tableView] setTableHeaderView:tableHeaderView];

    [tableHeaderView setNeedsLayout];
    [tableHeaderView release];

Here is my UIView subclass

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {

     UIView *headerView = self;

     self.headerTitle = [[UILabel alloc] initWithFrame:CGRectZero];
     self.headerTitle.numberOfLines = 3;
     self.headerTitle.lineBreakMode = UILineBreakModeWordWrap;
     [headerView addSubview:self.headerTitle];
     [self.headerTitle release];

     self.headerOption1 = [[UILabel alloc] initWithFrame:CGRectZero];
     self.headerOption1.numberOfLines = 2;
     self.headerOption1.lineBreakMode = UILineBreakModeWordWrap;
     [headerView addSubview:self.headerOption1];
     [self.headerOption1 release];
 }
 return self;
}

- (void)layoutSubviews {

 [super layoutSubviews];

 CGSize maxLabelSize;

 /*...*/

 [self.headerTitle setFrame:CGRectMake(10.0f, 10.0f, titleWidth, titleHeight)];

 /*...*/

 [self.headerOption1 setFrame:CGRectMake(10.0f, (self.headerTitle.frame.origin.y + self.headerTitle.bounds.size.height + 2.5f), pubWidth, pubHeight)];

    /*...*/
    [self setFrame:CGRectMake(0.0f, 0.0f, 320.0f, tableHeaderHeight)];
}

The second time that layoutSubViews is called all of the subviews get sized correctly except for the view itself (tableHeaderHeight has the correct height). Should I not be resizing the view from this method? Is there a better approach to this?

  • 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-17T20:22:19+00:00Added an answer on May 17, 2026 at 8:22 pm

    You likely need to override sizeThatFits: on your UIView subclass to return the appropriate size based on your layout.

    Use it like this:

    TableHeaderView *tableHeaderView = [[TableHeaderView alloc] initWithFrame:CGRectZero];
    tableHeaderView.headerTitle.text = title;
    tableHeaderView.headerOption1.text = headerOption1
    tableHeaderView.headerOption2.text = headerOption2
    tableHeaderView.headerOption3.text = headerOption3
    
    tableHeaderView.frame = (CGRect){
        .origin = tableHeaderView.frame.origin,
        .size = [tableHeaderView sizeThatFits:CGSizeZero],
    };
    
    [[self tableView] setTableHeaderView:tableHeaderView];
    
    [tableHeaderView setNeedsLayout]; // I don't think you need this anymore.
    [tableHeaderView release];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have a UIView subclass that stamps a single 2px by 2px CGLayerRef
I currently have a simple iPhone app that loads a custom subclass of UIView.
If I have a UIView (or UIView subclass) that is visible, how can I
I have the following code in a UIView subclass that will move it off
I have a UIView subclass that has the following in drawRect: for(int j=0; j<[myArray
I have a series of UIView subclasses that are added as subviews. Each subview
I have a View Controller that is swapping UIView objects in and out. There
I have a UIView subclass which currently assembles itself completely programatically. It has a
I currently have an MS Access application that connects to a PostgreSQL database via
I have created a custom UIView that I would like to use on multiple

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.