Hiding UIBarButtonItems

Discussion in 'iOS Programming' started by idelovski, Nov 10, 2010.

  1. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #1
    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?
     
  2. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #2
    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.
     
  3. idelovski thread starter macrumors regular

    Joined:
    Sep 11, 2008
    #3
    Thanks for the tip.
     

Share This Page