UIActivityIndicatorView Center in UIImageView

I created a custom subclass of UIImageView , and with one method I want to add an activity indicator to the center when the image loads. I am using the following code, but the activity monitor is located outside of UIImageView . What can I do to center it exactly within a UIImageView ?

 UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin; indicator.center = self.center; [self addSubview:indicator]; [indicator startAnimating]; 
+4
source share
3 answers

center property is the view coordinates in the coordinate space of its supervisor . Therefore, you need to set the coordinates of your indicator in the coordinate space of the imageView. The right way:

 indicator.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); 
+13
source
 indicator.center = [self convertPoint:self.center fromView:[self superview]]; 

You only need to convert the point because of this ( Link to the UIView class ):

@property (non-nuclear) CGPoint Center Discussion

The center is indicated in the coordinate system of its supervisor and is measured in points.

+3
source

indicator.center = imageView.center;

+2
source

Source: https://habr.com/ru/post/1414682/


All Articles