[英]Corner radius for table view cell
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
if (tableView == self.orderDetailsTableView)
{
//Top Left Right Corners
let maskPathTop = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.TopLeft, .TopRight], cornerRadii: CGSize(width: 5.0, height: 5.0))
let shapeLayerTop = CAShapeLayer()
shapeLayerTop.frame = cell.bounds
shapeLayerTop.path = maskPathTop.CGPath
//Bottom Left Right Corners
let maskPathBottom = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: 5.0, height: 5.0))
let shapeLayerBottom = CAShapeLayer()
shapeLayerBottom.frame = cell.bounds
shapeLayerBottom.path = maskPathBottom.CGPath
//All Corners
let maskPathAll = UIBezierPath(roundedRect: cell.bounds, byRoundingCorners: [.TopLeft, .TopRight, .BottomRight, .BottomLeft], cornerRadii: CGSize(width: 5.0, height: 5.0))
let shapeLayerAll = CAShapeLayer()
shapeLayerAll.frame = cell.bounds
shapeLayerAll.path = maskPathAll.CGPath
if (indexPath.row == 0 && indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1)
{
cell.layer.mask = shapeLayerAll
}
else if (indexPath.row == 0)
{
cell.layer.mask = shapeLayerTop
}
else if (indexPath.row == tableView.numberOfRowsInSection(indexPath.section)-1)
{
cell.layer.mask = shapeLayerBottom
}
}
}
what actually we are doing is if section has only one row then we do it on all sides, if section has multiple rows then we do it top on first row and bottom at last row... the properties BottomLeft, BottomRight, topLeft, TopRight should be of type rect corner(Suggestions from xcode when you are typing... there is another property content corner with same name.. so check that )实际上我们正在做的是如果部分只有一行,那么我们在所有方面都这样做,如果部分有多行,那么我们在第一行的顶部和最后一行的底部......属性BottomLeft,BottomRight,topLeft,TopRight应该是矩形角类型(输入时来自xcode的建议......还有另一个同名的属性内容角......所以检查一下)
You can use following code in swift 2.0您可以在 swift 2.0 中使用以下代码
Put this code in cellForRowAtIndexpath method:将此代码放在 cellForRowAtIndexpath 方法中:
cell!.layer.cornerRadius=10 //set corner radius here
cell!.layer.borderColor = UIColor.blackColor().CGColor // set cell border color here
cell!.layer.borderWidth = 2 // set border width here
Below is the output of my code下面是我的代码的输出
It seems that your tableView contains an UIView
so just add these rows in cellForRowAtIndexPath
.您的 tableView 似乎包含一个
UIView
,所以只需在cellForRowAtIndexPath
中添加这些行。 If not add an UIView and add the radius to the UIView and just add that view to your cell ( cell.addSubView(YOURVIEW)
).如果不添加 UIView 并将半径添加到 UIView 并将该视图添加到您的单元格(
cell.addSubView(YOURVIEW)
)。
cell.contentView.layer.cornerRadius = 10
cell.contentView.layer.masksToBounds = true
To customize the border you can要自定义边框,您可以
cell.layer.borderColor = UIColor.grayColor().CGColor
cell.layer.borderWidth = 5
Update更新
To add this in your viewForHeaderInSection
Create a view let view = UIView() and add the radius to your view在您的
viewForHeaderInSection
中添加它 创建一个视图 let view = UIView() 并将半径添加到您的视图
view.layer.cornerRadius = 10
view.layer.masksToBounds = true
And add other properties that you need and return that view.并添加您需要的其他属性并返回该视图。
try:尝试:
override func draw(_ rect: CGRect) {
super.draw(rect)
self.layer.cornerRadius = 8
self.clipsToBounds = true
}
in your TableViewCell在你的 TableViewCell
or:或者:
cell.layer.cornerRadius = 8
cell.clipsToBounds = true
Best way to do it is to change corner radius in awakeFromNib and make sure to set its clipToBounds to true.最好的方法是更改 awakeFromNib 中的角半径并确保将其 clipToBounds 设置为 true。
self.layer.cornerRadius = 4.0
self.clipToBounds = true
Or you can place a view inside your cell as background view and set its corner radius and clipToBounds.或者你可以在你的单元格中放置一个视图作为背景视图,并设置它的角半径和clipToBounds。
self.backgroundView.layer.cornerRadius = 4.0
self.backgroundView.clipToBounds = true
I was setting contentView.layer.cornerRadius = 10
and it was still not showing;我正在设置
contentView.layer.cornerRadius = 10
但它仍然没有显示; this was due to the cell.contentView.backgroundColor
defaulting to nil
which results in a transparent background color, thus it has cell.backgroundColor
as it's bkgColor.这是由于
cell.contentView.backgroundColor
默认为nil
,导致背景颜色透明,因此它具有cell.backgroundColor
,因为它是 bkgColor。
Setting the Cell's background color to .clear
, which in my case would show the tableView's background color (non-white), it would then show the corner radius as desired, like this:将单元格的背景颜色设置为
.clear
,在我的情况下将显示 tableView 的背景颜色(非白色),然后它会根据需要显示角半径,如下所示:
// Cell's init()
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
backgroundColor = .clear // this will show parent views bkgColor
//(in my case non-white)
// where the sharp corner is
contentView.layer.cornerRadius = 10.0 // set corner radius
contentView.backgroundColor = .white // set tableViewCells background color to what you want
/* do stuff */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.