将活动指示器添加到自定义按钮不起作用?

时间:2013-06-07 06:49:00

标签: iphone ios ipad

我有一个应用程序,我需要在点击按钮时在按钮上方添加活动指示器。我制作了所有自定义按钮,并将其添加到导航栏。

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];

但它没有出现。任何人都可以指出我哪里出错了吗?

3 个答案:

答案 0 :(得分:1)

在按钮操作中尝试此操作

UIButton * button = (UIButton *)sender;
UIActivityIndicatorView *myIndicator = [[UIActivityIndicatorView alloc]                                           initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[myIndicator setCenter:button.center];
[button addSubview:myIndicator];
[button bringSubviewToFront:myIndicator];
[myIndicator startAnimating];

答案 1 :(得分:1)

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

按钮操作前后 enter image description here enter image description here

答案 2 :(得分: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而不是按钮。