问题
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
回答1:
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
回答2:
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.
回答3:
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;
}
回答4:
Try this code in awakeFromNib:()
self.backgroundColor = [UIColor clearColor];
self.contentView.layer.cornerRadius =5;
self.contentView.layer.masksToBounds = YES;
回答5:
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 !
回答6:
Swift 4 :
override func layoutSubviews() {
super.layoutSubviews()
self.contentView.clipsToBounds = true
self.contentView.layer.cornerRadius = 5
}
来源:https://stackoverflow.com/questions/35355386/how-to-set-a-corner-radius-for-a-custom-cell