user3708224 user3708224 -4 years ago 58
iOS Question

issue centering activity indicator

I am having an issue centering an activity indicator view inside a subview, and I am not sure why. It is loading correctly, just not sure why it is not centered in the subview...thanks in advance!

controlsContainerView.frame = frame
addSubview(controlsContainerView)

controlsContainerView.addSubview(activityIndicatorView)
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true

Answer Source

centerXAnchor is a property of a view. It isn't clear what the view is in the equalTo part of your code:

activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true

Try explicitly naming the view:

activityIndicatorView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor)
.isActive = true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download