简体   繁体   中英

How to change UIBezierPath background color?

I have added these lines of code:

    var radius: CGFloat = UIScreen.mainScreen().bounds.width - 60
    let path: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height), cornerRadius: 0)

    let circlePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 30, y: (UIScreen.mainScreen().bounds.height - radius) / 2 - (navigationController?.navigationBar.frame.height)!, width: radius, height: radius), cornerRadius: radius)

    path.appendPath(circlePath)

but it gives me this result

在此处输入图片说明

How can I make my circle background color - clearColor() ?

I tried this one:

UIColor.whiteColor().setFill()
circlePath.fill()

But it did not make any sense

Try this:

let radius: CGFloat = 50
let path: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: 50, height: 50), cornerRadius: 0)

let circlePath: UIBezierPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: radius, height: radius), cornerRadius: radius)

path.append(circlePath)

let fillLayer = CAShapeLayer()
fillLayer.path = path.cgPath
fillLayer.fillColor = UIColor.red.cgColor
yourView.layer.addSublayer(fillLayer)

Paths do not have color; layers have color.

You need to create a layer based on a path, and add it to the layer of your view as shown in the code above.

try this:

let context = UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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