Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

idelovski

macrumors regular
Original poster
Sep 11, 2008
235
0
Looks like UIBarButtonItems don't have a property one can use to hide them. So, in order to hide such button I need to remove it from the toolbar. I have two methods that remove a rightmost button from a toolbar and put it back. The toolbar has two buttons and a flexible space between them.

The toolbar and everything else in the view is created in IB, the toolbar (bottomToolBar property) is an IBOutlet, but the button (restartButton property) isn't. I am setting it manualy in the code.

This is what I have now:
Code:
- (void)hideRestartBarButton
{
   NSUInteger  itemsInToolBar = [self.bottomToolBar.items count];
   
   if (itemsInToolBar == 3)  {
      NSMutableArray  *tmpItems = [self.bottomToolBar.items mutableCopy];
      self.restartButton = [tmpItems objectAtIndex:2];
      [tmpItems removeObjectAtIndex:2];
      self.bottomToolBar.items = tmpItems;
      [tmpItems release];
   }
}

- (void)showRestartBarButton
{
   NSUInteger  itemsInToolBar = [self.bottomToolBar.items count];
   
   if (self.restartButton && (itemsInToolBar == 2))  {
      NSMutableArray  *tmpItems = [self.bottomToolBar.items mutableCopy];
      
      [tmpItems addObject:self.restartButton];
      self.bottomToolBar.items = tmpItems;
      [tmpItems release];
   }
}

Is there a better way to do it?
 
Um, you can use the UIToolbar "setItems" method, so that it will animate the button fading in and out ;)

I'm shocked that UIBarButtonItem doesn't have a hidden property, so based on that I don't see any other way of doing it.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.