我找到了一篇关于如何在导航栏上的“Bar Button”中添加活动指示器的好文章。但是,在我的情况下,我无法重复此操作。我已经在代码中添加了导航栏和“UIBarButton”,但我找不到一个名为“UINavigationButton”的元素来放置活动指示器。
我希望UIBarButtonItem按钮是可见的:
而不是这样:
是否有人有建议如何使其正常工作?
我希望UIBarButtonItem按钮是可见的:
![enter image description here](https://istack.dev59.com/UKn31.webp)
![enter image description here](https://istack.dev59.com/EbLNf.webp)
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];
}