I am trying to use a Swift function to place a circle in the centre of a view so it is always on centre regardless of screen size. I can draw the circle at a point defined by a
First of all, UIViews have a center attribute, so you don't need to calculate it.
Second, the behavior may be inconsistent if changing it with viewDidLoad, and won't react to any changes to size/rotation.
I would recommend making a subClass of UIView that manages drawing the circle and doing something like this:
class ViewController: UIViewController {
var circle: CircleView?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
centerCircle()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
centerCircle()
}
func centerCircle() {
circle?.center = self.view.center
}
}