Activity Indicator within UINavigationBar
I've read a few answers about this particular topic, but for some reason, my code doesn't seem to work. It's within the UISearchBarDelegate delegate method -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
UIActivityIndicatorVi开发者_JAVA百科ew *activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem *activity = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[[self navigationItem] setRightBarButtonItem:activity];
[activity release];
[activityIndicator startAnimating];
Any help or suggestions are much appreciated!
I believe the designated initializer for UIActivityIndicatorView
is initWithActivityIndicatorStyle:
. Try creating activityIndicator
like this.
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
try this
UIView *view_actviti = [[UIView alloc] initWithFrame:CGRectMake(6,310,59,32)];
UIActivityIndicatorView *act =[[UIActivityIndicatorView alloc] init];
act.frame=CGRectMake(0, 0, 30, 30);
UIBarButtonItem *actItem = [[[UIBarButtonItem alloc] initWithCustomView:view_actviti] autorelease];
[view_actviti addSubview:act];
self.navigationItem.rightBarButtonItem = actItem;
[act startAnimating];
try this:
UIActivityIndicatorView *activityIndicator =
[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
UIBarButtonItem * barButton =
[[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[[self navigationItem] setRightBarButtonItem:barButton];
[barButton release];
[activityIndicator startAnimating];
Also use this for stopping and removing activityIndicator:
[[self navigationItem] setLRightBarButtonItem:nil];
[activityIndicator stopAnimating];
[activityIndicator release];
精彩评论