在UINavigationBar(iOS)上将UIActivityIndi​​catorView添加到UIBarButtonItem

Bor*_*zin 5 iphone uibarbuttonitem uiactivityindicatorview ios

我找到了一篇很棒的文章如何在导航栏上的Bar Button中添加一个Activity Indicator.但我不能在我的情况下重复这一点.我在代码中添加了导航栏和UIBarButton(不是在nib中),但我找不到一个名为UINavigationButton将Activity Indicator放入其中的元素.

我希望UIBarButtonItem按钮可见:

在此输入图像描述

而不是那样的:

在此输入图像描述

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

Mat*_*Mat 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];    

    }
Run Code Online (Sandbox Code Playgroud)

你会得到这个:

在此输入图像描述

编辑:
从您的评论看来,您似乎想在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];      
}
Run Code Online (Sandbox Code Playgroud)

你会得到这个:

在此输入图像描述