简体   繁体   English

UIBezierPath roundedRect-更改角的颜色

[英]UIBezierPath roundedRect - change color of corners

I have a simple rounded rect 我有一个简单的圆角矩形

override func draw(_ rect: CGRect) {
    let path = UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.allCorners, cornerRadii: CGSize(width: cornerSize, height: cornerSize))
    fillColor.setFill()
    path.fill()
    //fillColor is set to UIColor.black
    //cornerSize is set to 20
}

But the corners are white. 但是角落是白色的。 If I set background color in UIBuilder to clear color, the rounded rect becomes a rect with sharp corners. 如果我在UIBuilder中设置背景色以清除颜色,则圆角的rect会变成带有尖角的rect。 I tried to create an oval with rect and fill it with clear color. 我试图用rect创建一个椭圆形,并用清晰的颜色填充它。 This also didn't work. 这也没有用。 Maybe the answer is trivial but I'm absolutely clueless. 也许答案很简单,但我绝对一无所知。

在此处输入图片说明

Your code works fine as it is. 您的代码可以正常工作。 If it is drawing solid black when the views background color is clear then that probably means you have a black view of the same size behind your bezier view. 如果在视图背景颜色清晰的情况下以纯黑色绘制,则可能意味着您在贝塞尔曲线视图后面有一个相同大小的黑色视图。 You can test this by changing the fill color of your bezier curve to something other than black. 您可以通过将贝塞尔曲线的填充颜色更改为黑色以外的颜色来进行测试。

Its a lot easier to debug if you use the @IBDesignable and @IBInspectable keywords: 如果使用@IBDesignable和@IBInspectable关键字,调试起来会容易得多:

@IBDesignable class BezierView: UIView {

@IBInspectable var fillColor = UIColor.black

@IBInspectable var cornerRadius = 20.0

override func draw(_ rect: CGRect) {
    let path = UIBezierPath(roundedRect: rect, byRoundingCorners: UIRectCorner.allCorners, cornerRadii: CGSize(width: cornerRadius, height: cornerRadius))
    fillColor.setFill()
    path.fill()
}

} }

It will then draw the view in the storyboard and you can edit the fill color and corner radius in the views inspector. 然后它将在情节提要中绘制视图,并且您可以在视图检查器中编辑填充颜色和角半径。

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

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