I tried to centralize UIBezierPath
in a superview, but the UIBezierPath
is positioned at the bottom right, not at the center.
I set the value of anchorPoint
to (0.5, 0.5)
, position
self.view.center
or self.view.frame.width / 2
, but the UIBezierPath
is not centered.
import UIKit
/* ViewSection */
class ViewController: UIViewController {
@IBOutlet weak var Toolbar: UIToolbar!
@IBOutlet weak var UIToolbarButtonItem: UIBarButtonItem!
@IBAction func UIToolbarButtonItem(_ sender: Any) {
let objectFramePath = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: 150, height: 150), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
shapeLayer.position = self.view.center
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = objectFramePath.cgPath
self.view.layer.addSublayer(shapeLayer)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
The result is this: