5

我找到了一篇很棒的文章,如何在导航栏上的栏按钮内添加活动指示器。但在我的情况下,我不能重复这一点。我已经在代码中(不在笔尖中)添加了导航栏和 UIBarButton,但我找不到一个名为UINavigationButton将活动指示器放入其中的元素。

我希望 UIBarButtonItem 按钮可见:

在此处输入图像描述

而不是这样:

在此处输入图像描述

有没有人建议如何使这项工作?

4

2 回答 2

5

一个粗略的解决方法可能是这样的:

 act=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
 [act setFrame:CGRectMake(14, 5, 20, 20)];
 [act startAnimating];
 rightButt=[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:nil];
 self.navigationItem.rightBarButtonItem=rightButt;
 if ([[self.navigationController.navigationBar subviews] count]>=2) {
     //Be careful with the next line, Here you get access to an static index, 
     //Apple could change the structure of the navbar and your index may change in the future.
     [[[self.navigationController.navigationBar subviews] objectAtIndex:2] addSubview:act];    

    }

你会得到这个:

在此处输入图像描述

编辑
从你的评论看来,你想在 UIToolbar 中添加这个按钮,而不是在 UINavigationBar 中,它几乎是一样的:

UIActivityIndicatorView* act=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[act setFrame:CGRectMake(10, 13, 20, 20)];
[act startAnimating];
UIBarButtonItem *rightButt=[[UIBarButtonItem alloc] initWithTitle:@"      " style:UIBarButtonItemStyleBordered target:self action:nil];
UIToolbar *tb=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[tb setBarStyle:UIBarStyleBlack];
[tb setItems:[NSArray arrayWithObjects:rightButt, nil]];
if ([[tb subviews] count]>=1) {
    [[[tb subviews] objectAtIndex:1] addSubview:act];      
}

你会得到这个:

在此处输入图像描述

于 2012-05-06T13:56:59.947 回答
0

使用按钮并在其中添加活动指示器作为子视图

于 2012-05-06T09:48:28.927 回答