Add UIActivityIndicatorView to UIBarButtonItem in UINavigationBar (iOS)

I found a great article on how to add an activity indicator inside the Bar button on the navigation bar. But I cannot repeat it in my case. I added both the navigation bar and UIBarButton to the code (not in nib), but I could not find the element with the name UINavigationButtonto turn on the activity indicator inside.

I want the UIBarButtonItem button to be visible:

enter image description here

And not like that:

enter image description here

Does anyone have a suggestion on how to make this work?

+5
source share
2 answers

The rough work around could be something like this:

 act=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
 [act setFrame:CGRectMake(14, 5, 20, 20)];
 [act startAnimating];
 rightButt=[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:self action:nil];
 self.navigationItem.rightBarButtonItem=rightButt;
 if ([[self.navigationController.navigationBar subviews] count]>=2) {
     //Be careful with the next line, Here you get access to an static index, 
     //Apple could change the structure of the navbar and your index may change in the future.
     [[[self.navigationController.navigationBar subviews] objectAtIndex:2] addSubview:act];    

    }

And you get the following:

enter image description here

:
, UIToolbar, UINavigationBar, :

UIActivityIndicatorView* act=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[act setFrame:CGRectMake(10, 13, 20, 20)];
[act startAnimating];
UIBarButtonItem *rightButt=[[UIBarButtonItem alloc] initWithTitle:@"      " style:UIBarButtonItemStyleBordered target:self action:nil];
UIToolbar *tb=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[tb setBarStyle:UIBarStyleBlack];
[tb setItems:[NSArray arrayWithObjects:rightButt, nil]];
if ([[tb subviews] count]>=1) {
    [[[tb subviews] objectAtIndex:1] addSubview:act];      
}

:

enter image description here

+5

0

All Articles