Phillip
Phillip

Reputation: 4306

Get center of any UIView in Swift

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?

Upvotes: 14

Views: 38247

Answers (3)

Romain TAILLANDIER
Romain TAILLANDIER

Reputation: 2015

Original Answer:

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()


}

Updated Code for Swift 5.0:

  • From edit by Rob
  • 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()

}

Upvotes: 17

Gilad Brunfman
Gilad Brunfman

Reputation: 3502

Swift 3:

activityIndicator.center = CGPoint(x: view.width/2, y: view.height/2)

or you can try:

activityIndicator.center = view.center

Upvotes: 15

EmilioPelaez
EmilioPelaez

Reputation: 19922

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)

Upvotes: 8

Related Questions