简体   繁体   English

拐角半径仅适用于视图的左上角和左下角

[英]Corner radius only for top and bottom left corner of a view

I need to round corner only the top left and bottom left of a view, so i tried this: 我只需要在视图的左上角和左下角的圆角处,所以我尝试了这个:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "teamCell", for: indexPath) as! TeamCell

    let view = cell.backgroundCellView
    let rectShape = CAShapeLayer()
    rectShape.bounds = (view?.frame)!
    rectShape.position = (view?.center)!
    rectShape.path = UIBezierPath(roundedRect: (view?.bounds)!, byRoundingCorners: [.topRight, .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
    view?.layer.mask = rectShape
    view?.layer.masksToBounds = true

    return cell
}

and it worked very well, but after I put the constraints (trailing, leading, top and botton - i need a responsive view) only the topLeft corner is rounded and not the other. 并且它工作得非常好,但在我设置了约束(尾随,前导,顶部和按钮 - 我需要一个响应视图)后,只有topLeft角是圆形而不是另一个。 How could i fix it? 我怎么能解决它?

Starting with iOS 11, and Swift 4, you can use maskedCorners: 从iOS 11和Swift 4开始,您可以使用maskedCorners:

let myView = UIView()

myView.layer.cornerRadius = 15
myView.clipsToBounds = true

// Top Left Corner: .layerMinXMinYCorner
// Top Right Corner: .layerMaxXMinYCorner
// Bottom Left Corner: .layerMinXMaxYCorner
// Bottom Right Corner: .layerMaxXMaxYCorner
myView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]

try this one 试试这个

 let path = UIBezierPath(roundedRect:viewToRound.bounds, byRoundingCorners:[.TopLeftt, .BottomLeft], cornerRadii: CGSizeMake(20, 20))
    let maskLayer = CAShapeLayer()
    maskLayer.path = path.CGPath
    view.layer.mask = maskLayer

A simple way to do this is to set the corner radius of your cell's content view, and then to prevent the right side corners of the contents from being rounded you could constrain them to have x trailing space to the content view (where x is your corner radius). 一种简单的方法是设置单元格内容视图的角半径,然后防止内容的右侧角被舍入,您可以将它们约束为内容视图的x尾随空格(其中x是您的角半径)。 This does require you to adjust your layout to account for the extra padding on the right side of your cells though. 这确实需要您调整布局以考虑单元格右侧的额外填充。

Try using 尝试使用

view?.layer.cornerRadius = 15 

insted of 绝对的

rectShape.path = UIBezierPath(roundedRect: (view?.bounds)!, byRoundingCorners: [.topRight, .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM