Reputation: 705
I have a UIButton in which I added a UIActivtyIndicatorView when the user clicks on the button. The user clicks on the UIButton labeled "GO" and then the activityIndicator is supposed to come in and cover the "GO" title and spin until whatever is going on in the background stops.
- (void)going:(UIButton *)button {
NSLog(@"going button pressed");
goingActivity = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
goingActivity.center = button.center;
[button.superview insertSubview:goingActivity aboveSubview:button];
[goingActivity startAnimating];
if ([button.titleLabel.text isEqualToString:@"Go"]) { // Select
[PFObject saveAllInBackground:save block:^(BOOL success, NSError *error) {
//Do stuff
if (success) {
[self selectGoingButton:button]; // Toggle Button
selectedGoingToButton = button;
[defaults synchronize];
[self reloadData]; // Refresh tableview
}
}];
}
else { // Deselect
if (success) {
//Toggle button
selectedGoingToButton = nil;
[self deselectGoingButton:button];
[self reloadData];
}
}];
}
}
- (void)selectGoingButton:(UIButton *)button {
NSLog(@"GoingButtonselected");
[button setTitle:@"Going" forState:UIControlStateNormal];
button.layer.borderColor = neonBlue.CGColor;
[button setNeedsDisplay];
}
This is the code that handles the issue. I removed the meat of the code, but all that is doing is updating user info on parse, and on the disk so its not relevant. The issue is still that the going button title does not disappear while the activityIndicator is running (both the title and the spinner can be seen), and then the title switches to "GOING" and then the spinner stops.
I want it so that when the user clicks the button the activityIndicator starts, covers up the button title, and then the button title switches to "GOING" when the background activity stops. How can I make the activityIndicatorView cover the title layer?
EDIT: FOR CLARIFICATION, The activityIndicator is positioned correctly. It is in the middle of the button and can be seen there just fine. The issue is that it DOES NOT COVER the top layer of the button which has the title "GO". I want the title to disappear when I add the activityIndicator subview, but that is not happening with my current code.
Upvotes: 0
Views: 1332
Reputation: 705
I figured it out. It was rather simple actually I was just overthinking it.
- (void)going:(UIButton *)button {
goingActivity = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
goingActivity.center = button.center;
[button.superview insertSubview:goingActivity aboveSubview:button];
if ([button.titleLabel.text isEqualToString:@"Go"]) { // Select
[button setTitle:@"" forState:UIControlStateNormal];
[goingActivity startAnimating];
[PFObject saveAllInBackground:save block:^(BOOL success, NSError *error) {
//Do stuff
if (success) {
[self selectGoingButton:button]; // Toggle Button
selectedGoingToButton = button;
[defaults synchronize];
[self reloadData]; // Refresh tableview
}
}];
}
else { // Deselect
[button setTitle:@"" forState:UIControlStateNormal];
[goingActivity startAnimating];
if (success) {
//Toggle button
selectedGoingToButton = nil;
[self deselectGoingButton:button];
[self reloadData];
}
}];
}
}
All I did was once I was in the if-else, change the title of the button to nothing. And then from within each case start the activityIndicator.
Upvotes: 0
Reputation: 707
Not sure that this is the best way to do what you want to do here, but you can use the code you have if you add a test on 'selectGoingButton' check if the title is equal to 'Go'. if it is, set an empty title - @" ", if it is @" ", set it to @"Going", etc.
Upvotes: 0
Reputation: 708
try this
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGFloat halfButtonHeight = btnLogin.bounds.size.height / 2;
CGFloat buttonWidth = btnLogin.bounds.size.width;
indicator.center = CGPointMake(buttonWidth - halfButtonHeight , halfButtonHeight);
[btnLogin addSubview:indicator];
[indicator startAnimating];
Upvotes: 2