0
votes

I have a UIToolbar at the top of my view, and it needs to be resized in an animation. The toolbar contains:

  • A UIBarButtonItem using system item UIBarButtonSystemItemAdd (the '+' button)
  • A UIBarButtonItem using a custom view (the title)
  • A UIBarButtonItem using the style UIBarButtonItemStyleBordered (the 'Edit' button)

I am adding these buttons using a spacer between each, which keeps the title in the center:

    [toolbar setItems: @[addButton, spacer, titleButton, spacer, editButton] animated:NO];

Original Size

The toolbar resizes just fine, and the two buttons keep their locations pinned to the outside edges. However, the title button does not stay in the center of the toolbar. Instead, it seems to pin its right edge to the same location, creating a space on its left side. During the animation, this give the impression that it is sliding right.

Expanded Size

To be clear, I do want the title bar to keep its same width - I do not want it to expand as the toolbar grows. But I need the title to stay in the center of the toolbar.

Since a UIBarButtonItemis not a UIView, I can't (?) use the autoresizingmask functionality.

How do I keep the title in the center of the toolbar?

Additional Info

This might be because the Add and Edit buttons have different widths - when I add only the title (with a spacer either side), the behavior is correct.

2

2 Answers

0
votes

It turns out that I was leaving out a crucial detail. I am manually setting the width of the edit button to 50.

If this is removed, the resizing works as expected. However, I am then unable to control the size of the Edit button, which I like to set to 50 so it matches the one on the navigation bar.

I found a workable solution. Keep the width of the Edit button at 50. Add a fixed separator immediately after the '+' button, and set its width to 18 (50 - the width of the '+' button). This balances the items either side of the title.

0
votes

I know it's too bad, still you can use custom button as bar button item and use images of Add/Edit button in it. So that you can set the button frames, at any places. So your button won't placed to right most place.

UIButton * addButton = [[UIButton alloc] init];
[addButton setBackgroundImage:<AddimageName> forState:UIControlStateNormal];
UIBarButtonItem * addItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];