I'm trying to write a global function that adds an activityIndicator to any view (mostly imageViews though) by calling such function.
The function I have right now is made of:
public func addActivityIndicatorToView(activityIndicator: UIActivityIndicatorView, view: UIView){
//activityIndicator configuration ...
activityIndicator.center = view.center
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
}
But I don't get the center of the view for some reason.
I've also tried various solutions around SO and Google, but none worked until now.
Is there any way to get the center point and set the activityIndicator to any UIView?
Select your image view and apply width and height constraints via the pin dialogue box (screenshot below), then open your alignment constraints and select center horizontally and center vertically. Then just apply your constraints and voila!
I had reproduce your problem easily (see my comment). I think it could be a problem of auto layout. So may be you can use constraints instead calculate position ?
func addActivityIndicatorToView(activityIndicator: UIActivityIndicatorView, view: UIView){
self.view.addSubview(activityIndicator)
//Don't forget this line
activityIndicator.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
activityIndicator.startAnimating()
}
NSLayoutAttribute.CenterX
is now NSLayoutConstraint.Attribute.centerX
NSLayoutRelation.Equal
is now NSLayoutConstraint.Relation.equal
NSLayoutAttribute.CenterY
is now NSLayoutConstraint.Attribute.centerY
func addActivityIndicatorToView(activityIndicator: UIActivityIndicatorView, view: UIView){
self.view.addSubview(activityIndicator)
//Don't forget this line
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerX, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerX, multiplier: 1, constant: 0))
view.addConstraint(NSLayoutConstraint(item: activityIndicator, attribute: NSLayoutConstraint.Attribute.centerY, relatedBy: NSLayoutConstraint.Relation.equal, toItem: view, attribute: NSLayoutConstraint.Attribute.centerY, multiplier: 1, constant: 0))
activityIndicator.startAnimating()
}
Swift 3:
activityIndicator.center = CGPoint(x: view.width/2, y: view.height/2)
or you can try:
activityIndicator.center = view.center
The center
property is relative to a view's superview. So if your view's frame is {10, 10, 20, 20}
, center
is going to be {20, 20}
.
I'm guessing you want to center the activityIndicator
in view
.
You can do
activityIndicator.center = CGPointMake(view.width/2, view.height/2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With