我找到了 great article如何在导航栏上的栏按钮内添加事件指示器。但我不能在我的案例中重复这一点。我在代码中添加了 Navigation Bar 和 UIBarButton(不是在 nib 中),但是我找不到名为 UINavigationButton
的元素来将 Activity Indicator 放入其中。
我希望 UIBarButtonItem 按钮可见:
不是那样的:
有没有人对如何使这项工作有建议?
最佳答案
粗略的解决方法可能是这样的:
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];
}
你会得到这个:
关于ios - 将 UIActivityIndicatorView 添加到 UINavigationBar 上的 UIBarButtonItem (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469550/