Updating the caption constraints of cells

I have a custom cell and I am trying to update the subview constraints as shown below:

CustomeCell.m

-(void)layoutSubviews
{
    [super layoutSubviews];

    _con_view_width.constant = _lbl_property.frame.size.width;
    if(!_btn_imageCount.isHidden)
    _con_view_width.constant = _lbl_property.frame.size.width + _btn_imageCount.frame.size.width;

    NSLog(@"%@",NSStringFromCGRect(_view_lbl_btn.frame));

    [_view_lbl_btn updateConstraintsIfNeeded];
    NSLog(@"%@",NSStringFromCGRect(_view_lbl_btn.frame));

}

      

Problem The limitation only works after reloading rows while scrolling

+3


source to share


2 answers


Try layoutIfNeeded instead of updateConstraintsIfNeeded. i think it will work and your code should look like this.

-(void)layoutSubviews
{
    [super layoutSubviews];

    _con_view_width.constant = _lbl_property.frame.size.width;
    if(!_btn_imageCount.isHidden)
    _con_view_width.constant = _lbl_property.frame.size.width + _btn_imageCount.frame.size.width;

    NSLog(@"%@",NSStringFromCGRect(_view_lbl_btn.frame));

    [_view_lbl_btn layoutIfNeeded];
    NSLog(@"%@",NSStringFromCGRect(_view_lbl_btn.frame));

}

      



EDIT: If you are doing this inside a custom cell class, you need to add another row to the cell for the row at the pointer path.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    //[cell layoutIfNeeded];
    [cell layoutSubviews];
    return cell;
}

      

+3


source


try to update the constraints in the method used to return a cell in a tableview to a data source deletion instead of the layoutSubviews method in a cell.



And call cell.contentView.layoutIfNeeded()

before updating the constraints that will set the default cell width and height (320, 44 based on device) if you can get the width. After returning the cell to the table. The cell size will be updated by the table view (framework for yours) if you set some constraints that either directly or relatively resize.

+1


source







All Articles