更新单元子视图的约束

use*_*811 6 iphone objective-c uitableview ios autolayout

我有一个自定义单元格,我正在尝试更新子视图的约束,如下所示:

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));

}
Run Code Online (Sandbox Code Playgroud)

问题 约束仅在滚动时重新加载行后才起作用

Mah*_*wal 8

而不是updateConstraintsIfNeeded尝试layoutIfNeeded.我认为它会工作,你的代码应该是这样的.

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

}
Run Code Online (Sandbox Code Playgroud)

编辑:如果您在自定义单元格类中执行此操作,则需要在索引路径的行中为单元格添加一行.

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