38

I am calling the -(void)setEditing:(BOOL)editing animated:(BOOL)animated method in my code to swap between two buttons on the RHS of my navigation bar.

-(void)setEditing:(BOOL)editing animated:(BOOL)animated {  
  [super setEditing:editing animated:animated];  

  // Toggle ‘+’ and ‘Add To Order’ button.    

  if( editing ) {
    self.navigationItem.rightBarButtonItem = self.addItemButton;  
  }  
  else {           
    self.navigationItem.rightBarButtonItem = self.addToOrderButton;  
  } 
}

Where self.addItemButton and self.addToOrderButton are ivars containing predefined UIBarButtonItems, setup in awakefromNib.

The button in self.addToOrderButton is significantly wider then the one in self.addItemButton, so I’d like their to be a subtle animation between the two widths when the change in editing state is triggered (by tapping a standard editButtonItem on the LHS of the navigation.

If I surround the whole if:else with [UIView beginAnimations:nil context:NULL]; and [UIView commitAnimations]; the button change does animate, but with their positions—flying into place individually from the top left—rather than in place and just animating their widths.

How I animate the navigation bar elements so that each individual one (the RHS button, the title) animate in more appropriate, restrained ways?

1 Answer 1

78
self.navigationItem.rightBarButtonItem = self.addToOrderButton; 

There are specific methods you can use to animate the right and left bar button items:

 [self.navigationItem setRightBarButtonItem: self.addToOrderButton animated:YES];

...which will animate it for you. If you need everything to animate (including the title) you can also use the setItems:animated: method of your navigation bar (pass it in an array of the navigation items you'd like to display)

Sign up to request clarification or add additional context in comments.

7 Comments

I’ve accepted the answer, as that does exactly what you describe and works okay. Many thanks. As an aside, is there a method to tweak the actual animation that's occurring? As presented here, that does a cross-fade—which is fine—but a combination of cross-fade for the text and animated move of the button LHS for the ‘frame’ would be even better.
There's no direct method in the SDK, no. You could probably implement something yourself, but it would be non-trivial (something like overriding the method with a category and replacing it with your own transition).
Sounds like something that I would need to study the docs on; thanks for getting me on the right track though.
also if you don't know you can use: [self.navigationItem setLeftBarButtonItem:nil animated:YES]; this can be used in combination with: [self.navigationItem setHidesBackButton:NO animated:YES];
Before you try to use setItems:animated:, be aware that it cannot be used on the navigationController's navigationBar. You'll get slapped with the following exception: Cannot call setItems:animated: directly on a UINavigationBar managed by a controller.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.