如何将UIActivityIndicatorView微调器圆圈添加到UIBarButton中,以便当用户点击导航栏上的其中一个按钮时,他们会在加载时看到微调器?
答案 0 :(得分:42)
如果您尝试在导航栏按钮中显示活动轮(例如,导航栏上可能有刷新按钮) - 您可以创建一个新的UIBarButtonItem
,其自定义视图为{{1 }}:
<强>目标C 强>
UIActivityIndicatorView
<强>夫特强>
uiBusy = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
uiBusy.hidesWhenStopped = YES;
[uiBusy startAnimating];
[self.navigationItem.rightBarButtonItem initWithCustomView:uiBusy];
这会用旋转轮覆盖你的let uiBusy = UIActivityIndicatorView(activityIndicatorStyle: .White)
uiBusy.hidesWhenStopped = true
uiBusy.startAnimating()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: uiBusy)
。完成后,只需重新创建rightBarButtonItem
。
答案 1 :(得分:6)
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.hidesWhenStopped = YES;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
在需要的地方放置以下内容:
[activityIndicator startAnimating];
[activityIndicator stopAnimating];
答案 2 :(得分:3)
实际上,活动指示符未添加为工具栏项。这是当前观点的子视图。
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[act setCenter:CGPointMake(20, 20)];
[act startAnimating];
[self.view addSubview:act];
请务必在-(void)dealloc
中发布。
答案 3 :(得分:0)
使用此方法
-(void)startAniatingActivityIndicator{
@try {
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(self.window.frame.size.width-30, 32.5, activityIndicator.bounds.size.width, activityIndicator.bounds.size.height);
[self.window addSubview:activityIndicator];
[activityIndicator startAnimating];
[self.window bringSubviewToFront:activityIndicator];
}
@catch (NSException *exception) {
}
@finally {
}
}
-(void)stopAniatingActivityIndicator{
[activityIndicator stopAnimating];
[activityIndicator removeFromSuperview];
activityIndicator = nil;
}
答案 4 :(得分:-1)
UIActivityIndicatorView是一种视图。将其框架设置在您的按钮内,并使用-addSubview将其添加到UIBarButton的视图层次结构中。
我过于简单了,因为你必须尝试使它适合空间(可能通过缩放)并使其居中......
答案 5 :(得分:-2)
伪代码,我不会在Xcode中检查这个,但是这样的东西应该有效:
UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] init];
act.frame = CGMakeRect(3,3,25,25);
[myBarButton addSubview:act];
[act release];