53

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

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

4

12 回答 12

87

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

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self navigationItem].rightBarButtonItem = barButton;
[activityIndicator startAnimating];
于 2010-02-18T19:01:24.560 回答
27

SWIFT代码:

let activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
let barButton = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.setRightBarButton(barButton, animated: true)
activityIndicator.startAnimating()
于 2016-04-12T22:31:51.740 回答
13

您在这里创建了一个新的活动指示器视图,这很好,但您不是指状态栏中的活动指示器。

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

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
于 2010-02-18T14:59:21.727 回答
13

这在 Swift 中对我有用:

let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView.init(activityIndicatorStyle: .White)
let refreshBarButton: UIBarButtonItem = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.leftBarButtonItem = refreshBarButton
activityIndicator.startAnimating()
于 2016-07-15T21:54:42.010 回答
5

迅速

  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)
    }
    
于 2018-07-17T10:54:37.780 回答
4

在故事板上:BarButtonItem在导航栏上创建。添加 View 成为你的儿子和你BarButtonItemActivityIndicator儿子View

于 2014-12-15T12:51:11.190 回答
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
}
于 2017-11-21T08:10:34.417 回答
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];
  }
于 2014-07-10T11:01:25.153 回答
2

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

使能够: UIApplication.sharedApplication().networkActivityIndicatorVisible = true

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

于 2015-01-07T13:08:05.017 回答
1

我在这里有一个类似的问题: iphone - programatically change navigation bar button to activity indicator

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

于 2010-02-18T19:51:53.010 回答
1

尝试这个 :self.navigationItem.leftBarButtonItem.customView = your_view

于 2010-02-19T07:37:04.500 回答
0

我现在尝试使用它,但战士提到的代码并没有完全按原样工作。我不得不改变activityIndi​​cator的初始化:

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

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

于 2014-01-01T21:33:51.083 回答