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

时间:2010-02-18 14:33:37

标签: iphone frame activity-indicator navigationbar

我是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];
 }

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

12 个答案:

答案 0 :(得分:77)

我在视图中添加了以下代码,我想在导航栏中显示活动指示器。

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self navigationItem].rightBarButtonItem = barButton;
[activityIndicator startAnimating];

答案 1 :(得分:20)

Swift Code:

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()

答案 2 :(得分:13)

这在Swift中对我有用:

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

答案 3 :(得分:12)

您正在此处创建一个新的活动指示器视图,这很好,但您没有在状态栏中引用活动指示器。

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

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

答案 4 :(得分:3)

在故事板上:在导航栏中创建BarButtonItem。 添加视图以成为BarButtonItemActivityIndicator的儿子,成为View的儿子。

答案 5 :(得分:3)

像WhatsApp:

//声明

let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

// viewDidLoad中

self.activityIndicator.hidesWhenStopped = true

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

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

答案 6 :(得分:2)

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

我正在为其他新手分享一个代码示例,将其置于上下文中。

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

//为了完整性 - 这是我以编程方式显示/隐藏我的后退按钮的方式

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

答案 7 :(得分:2)

在Swift中,我做了以下事情:

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

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

答案 8 :(得分:2)

快速

  1. UIBarButtonItemstoryboard的网络yourViewController
  2. 从其定义中删除week,例如:@IBOutlet var btNavigaitonRight: UIBarButtonItem!
  3. 使用以下方法启动和停止活动指示器:

    var activityIndicator = UIActivityIndicatorView()
    
    func startBarButtonIndicator() {
        activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        activityIndicator?.color = .gray
        let barButton = UIBarButtonItem(customView: activityIndicator!)
        self.navigationItem.setRightBarButton(barButton, animated: true)
        activityIndicator?.startAnimating()
    }
    
    func stopBarButtonIndicator() {
        activityIndicator?.stopAnimating()
        navigationItem.setRightBarButton(btNavigaitonRight, animated: true)
    }
    

答案 9 :(得分:1)

我在回答中遇到了类似的问题: iphone - programatically change navigation bar button to activity indicator

我想将导航栏中的刷新按钮更改为活动指示器,然后再返回。

答案 10 :(得分:1)

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

答案 11 :(得分:0)

我现在尝试使用它,而warrior提到的代码并没有完全正常工作。我不得不改变activityIndi​​cator的初始化:

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

通过此更改,它应该按预期工作。