UITableViewCell layout width does not work with UIBezierPath

I'm having problems with auto layout restrictions in UITableViewCell when used with UIBezierPath. My cell will not become full width. You can see different images between image 1 and image 2. Image 2 I did not add rounding corners.

Image 1 With UIBezierPath

Image 2 Without UIBezierPath

I used to have the same problem with UIBezierPath in UiView (you can see the first 2 UIViews with "0"). The workaround I'm using is to mask the rounding angles in viewDidLayoutSubviews as shown below: -

- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; [self.view layoutIfNeeded]; UIView *container = (UIView *)[self.view viewWithTag:100101]; [container setBackgroundColor:[UIColor colorWithHexString:@"ffffff"]]; [self setMaskTo:container byRoundingCorners:UIRectCornerAllCorners]; } 

But now I'm stuck in a UITableViewCell, because I cannot add rounded corners to viewDidLayoutSubviews. My codes are as below: -

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"myData"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } MyWClass *w = [_wData objectAtIndex:indexPath.row]; UIView *container = (UIView *) [cell viewWithTag:200001]; [container setBackgroundColor:[UIColor colorWithHexString:w.bgColor]]; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:container.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0, 10.0)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = container.bounds; maskLayer.path = maskPath.CGPath; container.layer.mask = maskLayer; [cell setBackgroundColor:[UIColor colorWithHexString:@"#efeff4"]]; cell.layer.masksToBounds = NO; cell.layer.shadowOpacity = 1.0; cell.layer.shadowOffset = CGSizeMake(0, 2); cell.layer.shadowColor = [UIColor colorWithHexString:@"#e4e4e8"].CGColor; cell.layer.shadowRadius = 0; return cell; } 

I am trying to add [cell.contentView layoutIfNeeded]; , but still.

Any help would be appreciated.

+6
source share
1 answer

below logic works for me:

set the width of the UIview equal to the width of the cell.

Example: container.frame.size.width = cell.frame.size.width

0
source

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


All Articles