0

我有一个应用程序,只要单击它,我就需要在按钮上方添加活动指示器。我制作了所有这样的自定义按钮并将其添加到导航栏。`

UIButton *btnNext1 = [UIButton buttonWithType:UIButtonTypeCustom];
btnNext1.frame = CGRectMake(100, 100,38, 38);
UIImage *image = [UIImage imageNamed:@"default.png"]; 
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = image;
imageView.frame = CGRectMake(10,5,38,38); 
[imageView.layer setCornerRadius:5.0];// position it to the middle 
[btnNext1 setBackgroundImage:imageView.image forState:UIControlStateNormal];       
[imageView release]; 
[btnNext1 addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnNext =[[UIBarButtonItem alloc] initWithCustomView:btnNext1];

self.navigationItem.leftBarButtonItem = btnNext;
[btnNext release];

然后在我尝试的发件人方法中

UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] 
                                            initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];

// Add to button
[button addSubview:myIndicator];

// Start the animation
[myIndicator startAnimating];

但它没有出现。谁能指出我哪里出错了?

4

3 回答 3

1

我用你的代码试过它工作正常可能是图像和指示器都是相同的颜色或者你正在调用不同的方法,否则它工作正常。一旦检查下面的图像。

按钮操作之前和之后 在此处输入图像描述 在此处输入图像描述

于 2013-06-07T07:19:05.870 回答
1

在按钮操作中试试这个

UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]                                           initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myIndicator setCenter:button.center];
[button addSubview:myIndicator];
[button bringSubviewToFront:myIndicator];
[myIndicator startAnimating];
于 2013-06-07T06:55:45.477 回答
0

将您的发件人方法更改为

UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc] 
                                            initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

// Position the spinner
[myIndicator setCenter:CGPointMake(button.frame.size.width / 2,button.frame.size.height /2)];

// Add to button
[self addSubview:myIndicator];

// Start the animation
[myIndicator startAnimating];

您需要将指示器添加到 uiview 而不是按钮。

于 2013-06-07T07:01:14.953 回答