Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 552
Phillip Avatar asked May 12 '15 23:05

Phillip


People also ask

How do I center an image in Xcode?

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!


3 Answers

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

}
like image 121
Romain TAILLANDIER Avatar answered Sep 30 '22 03:09

Romain TAILLANDIER


Swift 3:

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

or you can try:

activityIndicator.center = view.center
like image 38
Gilad Brunfman Avatar answered Sep 30 '22 01:09

Gilad Brunfman


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)
like image 44
EmilioPelaez Avatar answered Sep 30 '22 01:09

EmilioPelaez