I’ve created a custom subclass of UIImageView and in one method I want to add an activity indicator to the center while the image is loading. I’m using the following code, but the activity monitor is positioned well outside of the UIImageView. What can I do to center it exactly within the bounds of the UIImageView?
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;
indicator.center = self.center;
[self addSubview:indicator];
[indicator startAnimating];
centerproperty is view’s coordinates in coordinate space of its superview. So you need to set coordinates of your indicator in the coordinate space of imageView. Correct way will be: