Resize a UIView subclass with dynamic-size substrings

I currently have a subclass of UIView that acts as my title for my UITableViewController. All subzones vary in size depending on the data received for a particular item.

layoutSubViews is called for a UIView before I can determine the size of each label. This causes a problem because I set the size of the view in the layoutSubViews method. Since it is called before setting my labels, the viewing height is always 0. Even after setting the labels, I call setNeedsLayout, but the size of the table title 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 subclass of UIView

- (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)];
}

, layoutSubViews , , (tableHeaderHeight ). ? ?

+3
2

, sizeThatFits: UIView, .

:

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];
+3

[tableHeaderView setNeedsLayout];

[tableHeaderView layoutSubviews];

. , setNeedsLayout layoutSubViews , . layoutSubViews, , , layoutSubviews.

, layoutSubviews - , ?

+1

Source: https://habr.com/ru/post/1770458/


All Articles