ios - 将 UIActivityIndi​​catorView 添加到 UINavigationBar 上的 UIBarButtonItem (iOS)

标签 ios iphone uibarbuttonitem uiactivityindicatorview

我找到了 great article如何在导航栏上的栏按钮内添加事件指示器。但我不能在我的案例中重复这一点。我在代码中添加了 Navigation Bar 和 UIBarButton(不是在 nib 中),但是我找不到名为 UINavigationButton 的元素来将 Activity Indicator 放入其中。

我希望 UIBarButtonItem 按钮可见:

enter image description here

不是那样的:

enter image description here

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

最佳答案

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

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

    }

你会得到这个:

enter image description here

编辑:
从你的评论来看,你似乎想在 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];      
}

你会得到这个:

enter image description here

关于ios - 将 UIActivityIndi​​catorView 添加到 UINavigationBar 上的 UIBarButtonItem (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10469550/

相关文章:

ios - 将字符串从 JSON 转换为 UIImage

ios - 无法在 iPod Touch 中部署 Xamarin 项目

iphone - Card.io SDK 给出部分信用卡号

iphone - 根据 iPhone 尺寸的特定类型自动布局 UIButton 和 UILabel 字体大小[非编程方式]

ios - 我可以在同一个 tableViewCell 中使用 IBOutlet 和 IBAction 吗?

iphone - 我可以从 kAudioSessionProperty_AudioRoute 属性返回什么样的路由?

ios - 在 Swift 中获取 UIBarButtonItem 的框架?

ios - 取消按钮而不是导航 Controller 中的后退按钮

ios - 条形按钮项目图像(UIImage)颜色随色调变化?

ios - 在 iOS 上,为什么 ViewController 中的 init 不起作用?