5

I have found a great article how to add an Activity Indicator inside Bar Button on Navigation Bar. But I cannot repeat this in my case. I have added both Navigation Bar and UIBarButton in code (not in nib) but I could not find an element named UINavigationButton to put Activity Indicator inside.

I want that UIBarButtonItem button is visible:

enter image description here

And not like that:

enter image description here

Does anyone have a suggestion how to make this work?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Borut Tomazin
  • 8,041
  • 11
  • 78
  • 91

2 Answers2

5

A 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'll get this:

enter image description here

EDIT:
From your comment it seems that you want to add this button inside UIToolbar, not in a UINavigationBar, it's pretty the same:

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

and you'll get this:

enter image description here

Mat
  • 7,613
  • 4
  • 40
  • 56
  • I have this button inside with other buttons so I'll have to make a different step. But probably this is it. I'll try and let you know. – Borut Tomazin May 06 '12 at 14:47
  • Unfortunately this doesn't work if UIBarButtonItem is inside UIToolbar... !? – Borut Tomazin May 10 '12 at 11:57
  • Nope, it's inside a uinavigationbar. And this works on my machine, what's the problem? – Mat May 10 '12 at 12:16
  • Exactly, you have placed it inside UINavigationBar, but I have it inside UIToolbar (three barButtonItems) that is also placed in UINavigationBar on the left/right side. The difference is I have wrapped buttons into UIToolbar instead directly to the navigationBar... Any suggestion? – Borut Tomazin May 10 '12 at 13:08
  • you've said: "I have added both Navigation Bar and UIBarButton in code", so my suggestions was for the navbar. But anyway, why a toolbar on a uinavbar? – Mat May 10 '12 at 13:28
  • cos I have more than one button inside Toolbar. As you know you left/right barButtonItem can only hold one. – Borut Tomazin May 10 '12 at 13:38
  • No, this works on iOS 5 too, there is no new/deprecated features in this. – Mat May 11 '12 at 08:37
0

use a button and add activity indicator as subview in it

Saad
  • 8,857
  • 2
  • 41
  • 51
  • This is not what I want. I have added extra explanation in my question. Check it out... – Borut Tomazin May 06 '12 at 10:12
  • ok this may be done by two aproaches, one is place the indicator on bar and reduce the alpha of button so that indicator partially displays, and when it disabled, indicator will be fully visible. – Saad May 06 '12 at 10:18
  • 2nd aproach is to add two indicators, one behind the button on bar, and one inside the button as subview, this time don't reduce the button's alpha, when u disable it, the backIndicator will be autometiclly visible – Saad May 06 '12 at 10:20
  • I do not have a problem showing indicatory in navigation bar. Instead showing it inside bar button item with button visible. The problem is that whenever you initWithCustomView than the button itself is hidden. Unfortunately none of your suggestions works. – Borut Tomazin May 06 '12 at 10:46
  • like UIButton* btn= [UIButton alloc] init]; [btn addSubview:activityIndicator]; self.navigationbarButton = btn; complete other parts of code – Saad May 06 '12 at 10:48
  • 1
    Yeah, that's what I did. But I cannot assign btn to navigationBarButton - Incompatible pointer types assigning to 'UIBarButtonItem *' from 'UIButton *__strong' – Borut Tomazin May 06 '12 at 11:32
  • also http://stackoverflow.com/questions/2848055/add-button-to-navigationbar-programatically – Saad May 06 '12 at 11:47
  • I know how to add buttons on navBar but look at the question above - I need indicator inside bar button which should still have border look... – Borut Tomazin May 06 '12 at 12:14