Frage

Ich bin neu in iPhone Entwicklung. Ich möchte eine Aktivitätsanzeige in der Navigationsleiste setzen. Ich sehe meine Aktivitätsanzeige unterhalb der Navigationsleiste. Mein Code ist hier

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

Wie kann ich meine Aktivitätsanzeige in der Navigationsleiste? Bitte hilf mir. Danke.

War es hilfreich?

Lösung

Ich füge die folgende Stück Code in der Ansicht, in der ich die Aktivitätsanzeige in der Navigationsleiste wollte.

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

Andere Tipps

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

Sie sind hier, um eine neue Aktivitätsindikator Ansicht erstellen, was in Ordnung ist, aber sie beziehen sich nicht auf die Aktivitätsanzeige in der Statusleiste.

Um die Aktivitätsanzeige in der Statusleiste zu zeigen, einfach diese nennen:

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

Das ist für mich in Swift gearbeitet:

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

Ein Storyboard: Erstellen BarButtonItem in der Navigationsleiste. Hinzufügen um zu seinen Sohn deiner BarButtonItem und ActivityIndicator Sohn deines View sein.

Wie WhatsApp:

// Declare

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
}

Danke! Mein Indikator arbeitet jetzt.

Ich bin ein Codebeispiel für fellow noobs teilen, setzen diese in Zusammenhang.

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

// Für Vollständigkeit - das ist, wie ich programmatisch zeigen / unterdr Zurück-Taste

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

// Ich verwende meine Aktivitätsanzeige mit einem UIWebView so Trigger es wie folgt:

  - (void)webViewDidStartLoad:(UIWebView *)webView
  {    
    [[self activityIndicator] startAnimating];
  }

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

  - (void)webViewDidFinishLoad:(UIWebView *) webView
  {
     [[self activityIndicator] stopAnimating];
  }

In Swift, habe ich die folgenden:

Aktivieren: UIApplication.sharedApplication().networkActivityIndicatorVisible = true

Deaktivieren: UIApplication.sharedApplication().networkActivityIndicatorVisible = false

Swift

  1. Connet UIBarButtonItem von storyboard zu yourViewController
  2. Entfernen week aus seiner Definition wie: @IBOutlet var btNavigaitonRight: UIBarButtonItem!
  3. Mit diesen Methoden für Start und Stopp-Aktivitätsanzeige:

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

hatte ich eine ähnliche Frage mit Antwort hier: iphone - programmatisch ändern Navigationsleiste Taste, um Aktivitätsanzeige

Ich wollte den Refresh-Button in der Navigationsleiste auf die Aktivitätsanzeige ändern und wieder zurück.

versuchen, dieses: self.navigationItem.leftBarButtonItem.customView = your_view

Ich habe versucht, es jetzt zu verwenden, und der Code erwähnt von Krieger nicht genau so funktionierte ist. Ich hatte die Initialisierung von activityIndicator zu ändern:

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

Mit dieser Änderung wie erwartet funktionieren soll.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top