ios - 将 UIActivityIndi​​catorView 添加到 UIButton 的顶部 View

标签 ios objective-c uibutton uiactivityindicatorview

我有一个 UIButton,当用户单击该按钮时,我在其中添加了一个 UIActivtyIndi​​catorView。用户单击标有“GO”的 UIButton,然后 activityIndi​​cator 应该进入并覆盖“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];
}

这是处理问题的代码。我删除了代码的内容,但所做的只是更新解析和磁盘上的用户信息,因此它不相关。问题仍然是在 activityIndi​​cator 运行时前进按钮标题不会消失(标题和微调器都可以看到),然后标题切换为“GOING”然后微调器停止。

我想要它,以便当用户单击按钮时,activityIndi​​cator 启动,覆盖按钮标题,然后当后台事件停止时,按钮标题切换为“GOING”。如何让activityIndi​​catorView覆盖title层?

编辑:为澄清起见,activityIndi​​cator 的位置正确。它在按钮的中间,在那里可以很好地看到。问题是它没有覆盖标题为“GO”的按钮的顶层。当我添加 activityIndi​​cator 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 - 将 UIActivityIndi​​catorView 添加到 UIButton 的顶部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24803311/

相关文章:

iOS Facebook 无法在其他用户的墙上发帖

iphone - iOS 4.3、iPhone 4 不使用 UILocalNotification 播放自定义声音

ios - Xcode 不显示 UIButton 的 @IBAction

ios - 如何让多个 View 使用同一个 Controller 类?

xcode - 为什么我不能从我的 XCode 项目中删除文件?

iOS 7 状态栏回到 iOS 6 风格的问题

ios - 如何在 SCLAlertView showWaiting 警报后重定向到主页?

ios - UIButton 选定文本未显示选定状态

ios - 如何在 iOS Swift 中获取实际的日期时间?即使用户从设备设置中更改了日期时间

ios - 从 Firebase 中删除 Google 或 Facebook 用户