Chandrika Visvesh
Chandrika Visvesh

Reputation: 91

How to set a corner radius for a custom cell

enter image description here

Here is my sample output ,in that below details there are three cells ,i have set corner radius for each custom cells.but it not working correctly .The corner radius for first cell is not working ,for the second cell the top right and top left is working ,please say what to do for this

   -(void)layoutSubviews
     {
[super layoutSubviews];
self.backgroundColor = [UIColor clearColor];
self.contentView.layer.cornerRadius =5;
self.contentView.layer.masksToBounds = YES;

 }

i have applied this above code to my cell class file

Upvotes: 4

Views: 12231

Answers (7)

This is an old question but I want to add my solution. If anybody struggles on this problem future. I wrote my code to willDisplayCell() as dev_m suggested in his comment.

Then can you try this in table view delegate method tableView:willDisplayCell:forRowAtIndexPath: method cell.backgroundColor = [UIColor clearColor]; cell.contentView.layer.cornerRadius =5; cell.contentView.layer.masksToBounds = YES;

But I added a layoutIfNeeded property on top of that. So my code is:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    cell.layoutIfNeeded()
    let leaveCell = cell as! LeaveItem
    leaveCell.vContainerRoot?.applyShadowWithCornerRadius(opacity: 0.5, radius: 20, edge: AIEdge.All, shadowSpace: 0.1)
    leaveCell.vLeaveItemHeader?.roundCorners(corners: [.topLeft, .topRight], radius: 20)
}

applyShadowWithCornerRadius and roundCorners methods are my extension functions.

Upvotes: 1

Mohammad Razipour
Mohammad Razipour

Reputation: 3701

Swift 4 :

override func layoutSubviews() {
    super.layoutSubviews()

    self.contentView.clipsToBounds = true
    self.contentView.layer.cornerRadius = 5

}

Upvotes: -1

Genevios
Genevios

Reputation: 1145

For me it's works perfectly, maybe my code helps to somebody.

NSInteger rowsAmount = [tableView numberOfRowsInSection:indexPath.row];
if (rowsAmount) {

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:( UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];

    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.view.bounds;
    maskLayer.path  = maskPath.CGPath;

    cell.layer.mask = maskLayer;

}

Upvotes: 1

Vvk
Vvk

Reputation: 4043

Other Simple Option is set RuntimeAttributs in Storyboard or XIB.

first select your view(click on your view) > go to Identiti Inspector (near Attributes inspector in your Xcode right side) > User Defined Runtime Attributes > + click and Add Below two one by one in this field.

layer.cornerRadius
layer.masksToBounds

Clean and build your project. not need to code any .h or .m file.

Upvotes: 4

Reshmi Majumder
Reshmi Majumder

Reputation: 961

In cellForRowAtIndexPath Use

    cell.layer.cornerRadius=5

If you have different cells in different sections then make sure that this code is written for all sections

Upvotes: 5

Pulkit Sharma
Pulkit Sharma

Reputation: 555

Here's a different approach : In your cellForRow just before returning the cell you can try out the following code :

CGFloat corner = 20.0f;
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:cell.backgroundView.bounds byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(corner, corner)];
CAShapeLayer  *shapeLayer = (CAShapeLayer *)cell.backgroundView.layer;
shapeLayer.path = path.CGPath;
shapeLayer.fillColor = cell.textLabel.backgroundColor.CGColor;
shapeLayer.strokeColor = [UIColor lightGrayColor].CGColor;
shapeLayer.lineWidth = 1.0f;

Let me know !

Upvotes: 1

dev_m
dev_m

Reputation: 806

Try this code in awakeFromNib:()

self.backgroundColor = [UIColor clearColor];
self.contentView.layer.cornerRadius =5;
self.contentView.layer.masksToBounds = YES;

Upvotes: 1

Related Questions