Search code examples
iosobjective-cuitableviewautolayoutios-autolayout

calayer did not change according to UIView size in tableview cell


There is a view1(UIView) in Custom UITableViewCell in I want new size of view1(UIView) after AutoLayout to draw border

UITableViewCell content is dynamic, hence I usedUITableViewAutomaticDimension for self resizing

I also use below methods but I get Original Size. I can not get new size of view1(UIView) after expanding

[self.tblView1 setNeedsLayout];
[self.tblView1 layoutIfNeeded]; 
[cell.view1 setNeedsLayout];
[cell.view1 layoutIfNeeded];

CALayer *rightBorder = [CALayer layer];
            rightBorder.backgroundColor = [[UIColor redColor] CGColor];
            rightBorder.frame = CGRectMake(cell.view1.frame.size.width-1, 0, 1, cell.view1.frame.size.height);
            [cell.view1.layer addSublayer:rightBorder];

rightBorder is draw but according to view1's(UIView) original size that is Width = 394 Height = 322 not according to view1's(UIView) new size after layout size that is Width = 394 Height = 422


Solution

  • Try this.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    if(tableView.tag == self.tblEditorialCalendar.tag)
    {
    
    UIView *leftBorder = [UIView new];
    leftBorder.backgroundColor = [UIColor redColor];
    leftBorder.translatesAutoresizingMaskIntoConstraints = NO;
    
    UIView *topBorder = [UIView new];
    topBorder.backgroundColor = [UIColor greenColor];
    topBorder.translatesAutoresizingMaskIntoConstraints = NO;
    
    UIView *rightBorder = [UIView new];
    rightBorder.backgroundColor = [UIColor blueColor];
    rightBorder.translatesAutoresizingMaskIntoConstraints = NO;
    
    UIView *bottomBorder = [UIView new];
    bottomBorder.backgroundColor = [UIColor yellowColor];
    bottomBorder.translatesAutoresizingMaskIntoConstraints = NO;
    
    [cell addSubview:leftBorder];
    [cell addSubview:topBorder];
    [cell addSubview:rightBorder];
    [cell addSubview:bottomBorder];
    
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[leftBorder(3)]"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(leftBorder)]];
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[leftBorder]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(leftBorder)]];
    
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-3-[topBorder]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(topBorder)]];
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[topBorder(1)]"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(topBorder)]];
    
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[rightBorder(1)]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(rightBorder)]];
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[rightBorder]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(rightBorder)]];
    
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-3-[bottomBorder]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(bottomBorder)]];
    [cell addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[bottomBorder(1)]|"
                                                                 options:0
                                                                 metrics:nil
                                                                   views:NSDictionaryOfVariableBindings(bottomBorder)]];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    }
    
    }
    

    Another way is to use layout anchor (supported from iOS 9 and above) instead of VFL

    [leftBorder.widthAnchor constraintEqualToConstant:3].active = YES;
    [cell.leadingAnchor constraintEqualToAnchor:leftBorder.leadingAnchor constant:0].active = YES;
    [cell.topAnchor constraintEqualToAnchor:leftBorder.topAnchor constant:0].active = YES;
    [cell.bottomAnchor constraintEqualToAnchor:leftBorder.bottomAnchor constant:0].active = YES;
    
    [topBorder.heightAnchor constraintEqualToConstant:1].active = YES;
    [cell.leadingAnchor constraintEqualToAnchor:topBorder.leadingAnchor constant:3].active = YES;
    [cell.topAnchor constraintEqualToAnchor:topBorder.topAnchor constant:0].active = YES;
    [cell.trailingAnchor constraintEqualToAnchor:topBorder.trailingAnchor constant:0].active = YES;
    
    [rightBorder.widthAnchor constraintEqualToConstant:1].active = YES;
    [cell.trailingAnchor constraintEqualToAnchor:rightBorder.trailingAnchor constant:0].active = YES;
    [cell.topAnchor constraintEqualToAnchor:rightBorder.topAnchor constant:0].active = YES;
    [cell.bottomAnchor constraintEqualToAnchor:rightBorder.bottomAnchor constant:0].active = YES;
    
    [bottomBorder.heightAnchor constraintEqualToConstant:1].active = YES;
    [cell.leadingAnchor constraintEqualToAnchor:bottomBorder.leadingAnchor constant:3].active = YES;
    [cell.trailingAnchor constraintEqualToAnchor:bottomBorder.trailingAnchor constant:0].active = YES;
    [cell.bottomAnchor constraintEqualToAnchor:bottomBorder.bottomAnchor constant:0].active = YES;
    

    Or you could just take four view (as four borders) in your nib file and place them with proper constraints.