Minkle Garg
Minkle Garg

Reputation: 751

UIBezierPath on UiView doesn't update until scroll the tableview

I have added UIView to cell of Tableview and given the beizer path to bottom and right by following:

UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,
                          0,
                          cell.bgView.frame.size.width -2+ shadowSize,
                          cell.bgView.frame.size.height+1 + shadowSize)];
    cell.bgView.layer.masksToBounds = NO;
    cell.bgView.layer.shadowColor = [[UIColor colorWithRed:186.0/255.0 green:0.0/255.0 blue:231.0/255.0 alpha:1.0f]CGColor];
    cell.bgView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    cell.bgView.layer.shadowOpacity = 0.7f;
    cell.bgView.layer.shadowPath = shadowPath.CGPath;

First time, when tableview reload, bezier path doesn't load properly

See Picture 1

but when I scroll up/down then it looks perfectly

See Picture 2.

I have tried with every possible solution i.e.

dispatch_async(dispatch_get_main_queue(), ^{
                [self.topRatedTable reloadData];
            });

or

 [cell layoutIfNeeded];
    [cell updateConstraintsIfNeeded];

But nothing works.

Upvotes: 3

Views: 617

Answers (2)

Maxvale
Maxvale

Reputation: 173

Use this:

  override func draw(_ rect: CGRect) {
        super.draw(rect)
        // UIBezierPath job
    }

Upvotes: 1

rob mayoff
rob mayoff

Reputation: 386018

Your cell and its subviews (like, I assume, bgView) don't have their final frames until layout has run. If your path-creating code is in your tableView:cellForRowAtIndexPath: method, then you are accessing cell.bgView.frame before layout has run.

In your custom UITableViewCell subclass, override layoutSubviews and set the shadowPath after calling super:

// In your custom `UITableViewCell` subclass:
- (void)layoutSubviews {
    [super layoutSubviews];
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0,
                      0,
                      cell.bgView.frame.size.width -2+ shadowSize,
                      cell.bgView.frame.size.height+1 + shadowSize)];
    self.bgView.layer.shadowPath = shadowPath.CGPath;
}

Upvotes: 3

Related Questions