Note that there are some explanatory texts on larger screens.

plurals
  1. POResizing a UIView subclass with dynamic sized subviews
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>This will create my TableHeaderView and set the text for my labels.</p> <pre><code> 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]; </code></pre> <p>Here is my UIView subclass</p> <pre><code>- (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)]; } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload