Reputation: 751
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
but when I scroll up/down then it looks perfectly
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
Reputation: 173
Use this:
override func draw(_ rect: CGRect) {
super.draw(rect)
// UIBezierPath job
}
Upvotes: 1
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