我有一个 UIButton,当用户单击该按钮时,我在其中添加了一个 UIActivtyIndicatorView。用户单击标有“GO”的 UIButton,然后 activityIndicator 应该进入并覆盖“GO”标题并旋转,直到后台发生的任何事情停止。
- (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];
}
这是处理问题的代码。我删除了代码的内容,但所做的只是更新解析和磁盘上的用户信息,因此它不相关。问题仍然是在 activityIndicator 运行时前进按钮标题不会消失(标题和微调器都可以看到),然后标题切换为“GOING”然后微调器停止。
我想要它,以便当用户单击按钮时,activityIndicator 启动,覆盖按钮标题,然后当后台事件停止时,按钮标题切换为“GOING”。如何让activityIndicatorView覆盖title层?
编辑:为澄清起见,activityIndicator 的位置正确。它在按钮的中间,在那里可以很好地看到。问题是它没有覆盖标题为“GO”的按钮的顶层。当我添加 activityIndicator subview 时,我希望标题消失,但我当前的代码并没有发生这种情况。
最佳答案
试试这个
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];
关于ios - 将 UIActivityIndicatorView 添加到 UIButton 的顶部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803311/