gpt4 book ai didi

ios - calayer 没有根据 tableview 单元格中的 UIView 大小而改变

转载 作者:行者123 更新时间:2023-11-29 00:22:13 25 4
gpt4 key购买 nike

Custom UITableViewCell 中有一个view1(UIView) 在AutoLayout 之后我想要新尺寸的view1(UIView) 来绘制边框

UITableViewCell 内容是动态的,因此我使用 UITableViewAutomaticDimension 来自动调整大小

我也使用以下方法,但我得到的是原始尺寸。扩展后我无法获得新的 view1(UIView) 大小

[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 是根据 view1's(UIView) 原始大小绘制的,即 Width = 394 Height = 322 不是根据 view1's(UIView) 布局后的新大小,即 Width = 394 Height = 422

最佳答案

试试这个。

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

}

另一种方法是使用布局 anchor (iOS 9 及更高版本支持)而不是 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;

或者您可以在您的 nib 文件中使用四个 View (如四个边框),然后将它们放置在适当的约束条件下。

关于ios - calayer 没有根据 tableview 单元格中的 UIView 大小而改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44016239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com