I'm trying to add Spinner to Like button that I have positioned in my tableViewCell. But the problem is spinner is showing outside the tableViewCell.
This is how I implemeneted my code inside cellForRowAtIndexPath
myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[myspinner setCenter:cell.like.center];
[cell.like addSubview:myspinner];
And when I click using sender.tag
[myspinner startAnimating];
Problem is spinner working but not as I wanted. It's showing outside the cell.
UPDATE
With matt
's answer It did work.
also i changed my code like below. inside selector action. -(void) likeClicked:(UIButton*)sender
UIActivityIndicatorView *myspinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[myspinner setCenter: CGPointMake(CGRectGetMidX(sender.bounds),
CGRectGetMidY(sender.bounds))];
[sender addSubview:myspinner];
[myspinner startAnimating];
or you may want to use cell's content view to get centre of cell,
this will work too.
Code of this form:
...is never right. The reason is that
myspinner.center
is in its superview's coordinates — that is, incell.like
coordinates. Butcell.like.center
is in its superview's coordinates. Thus, you are comparing apples with oranges: you are setting a point in terms of another point that lives in a completely different coordinate system. That can only work by accident.What you want to do is set
myspinner.center
to the center of its superview's bounds. Those values are in the same coordinate system (here,cell.like
's coordinate system).