如何在导航栏中设置活动指示器?

我是iphone开发的新手。我想在导航栏中设置一个活动指示器。我在导航栏下面看到我的活动指示器。我的代码在这里

-(IBAction) gomethod : (id) sender { xxMapSubviewcontroller = [[XxMapSubviewcontroller alloc] init]; [self.navigationController pushViewController:xxMapSubviewcontroller animated:YES]; activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activityIndicator.frame = CGRectMake(0.0, 0.0, 20.0, 20.0); [activityIndicator startAnimating]; [xxMapSubviewcontroller.view addSubview:activityIndicator]; } 

我如何设置我的活动指标在导航栏? 请帮我。谢谢。

我在视图中添加下面的一段代码,我想要导航栏中的活动指示器。

  activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator]; [self navigationItem].rightBarButtonItem = barButton; [activityIndicator startAnimating]; 

您正在创build一个新的活动指标视图,这很好,但是您并不是指状态栏中的活动指标。

要在状态栏中显示活动指示器,只需调用以下命令:

 [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

SWIFT代码:

 let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20)) let barButton = UIBarButtonItem(customView: activityIndicator) self.navigationItem.setRightBarButtonItem(barButton, animated: true) activityIndicator.startAnimating() 

这在Swift中适用于我:

 let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .White) let refreshBarButton: UIBarButtonItem = UIBarButtonItem(customView: activityIndicator) self.navigationItem.leftBarButtonItem = refreshBarButton activityIndicator.startAnimating() 

谢谢! 我的指标现在正在工作。

我为大家分享一个代码示例,把它放在上下文中。

– (void)viewDidLoad

 // custom button images UIImage *customImage = [UIImage imageNamed:@"menu24"]; UIImage *customImage2 = [UIImage imageNamed:@"search24"]; UIImage *customImage3 = [UIImage imageNamed:@"back24"]; // These are linked in my story board to Navigation Item [self customiseBarBtnItem:[self menu_button] customImage:customImage selector:@selector(menuPressed:)]; [self customiseBarBtnItem:[self search_button] customImage:customImage2 selector:@selector(searchPressed:)]; [self customiseBarBtnItem:[self backButton] customImage:customImage3 selector:@selector(backPressed:)]; //initialize the activity indicator - as @antf comment suggests for ios7 UIActivityIndicatorView *actInd=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; //store it as a property on the view controller self.activityIndicator = actInd; // this sets up activity indicator UIBarButtonItem *progress_indicator = [[UIBarButtonItem alloc] initWithCustomView:[self activityIndicator]]; // link custom buttons AND activity indicator in desired order to navigation bar self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects: self.menu_button, self.search_button, progress_indicator, nil]; 

/ /为了完整性 – 这是我如何以编程方式显示/隐藏我的后退button

 if ( bShowBack == YES ) self.navItemBar.leftBarButtonItem = self.backButton; else self.navItemBar.leftBarButtonItem = Nil; 

/ /我用我的活动指标与UIWebView所以触发它如下

  - (void)webViewDidStartLoad:(UIWebView *)webView { [[self activityIndicator] startAnimating]; } - (void)didFailLoadWithError:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [[self activityIndicator] stopAnimating]; } - (void)webViewDidFinishLoad:(UIWebView *) webView { [[self activityIndicator] stopAnimating]; } 

在故事板上:在导航栏上创buildBarButtonItem 。 添加视图是你的BarButtonItemActivityIndicator儿子是你的View儿子。

我有一个类似的问题,在这里回应: iphone – 以编程方式将导航栏button更改为活动指示器

我想将导航栏中的刷新button更改为活动指示灯并再次返回。

尝试。 self.navigationItem.leftBarButtonItem.customView =您的视图

在Swift中,我做了以下工作:

启用: UIApplication.sharedApplication().networkActivityIndicatorVisible = true

禁用: UIApplication.sharedApplication().networkActivityIndicatorVisible = false

我现在试图使用它,而战士所提到的代码并不完全正确。 我不得不改变activityIndi​​cator的初始化:

 activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

有了这个改变,它应该按预期工作。

像WhatsApp:

//声明let activityIndi​​cator = UIActivityIndi​​catorView(activityIndi​​catorStyle:.gray)

// viewDidLoad中

self.activityIndi​​cator.hidesWhenStopped =真

func showIndicator(){

  self.navigationItem.titleView = self.activityIndicator self.activityIndicator.isHidden = false } func hideIndicator() { self.navigationItem.titleView = nil }