How to test if item in toolBar is FlexibleSpace item?

871 Views Asked by At

I want to know which UIBarButtonItem enumerated in self.toolbarItems is a button and which is flexible space item.

3

There are 3 best solutions below

0
Lukasz 'Severiaan' Grela On BEST ANSWER

As A-Live confirmed my findings that one is not able to query the UIBarButtonItem to check if it is FlexibleSpace (or FixedSpace) I've used tag to mark those items as flexible and fixed space (2 different integers) and put those numbers in the constant then in code I use:

for(int i=0; i<self.toolbarItems.count; i++)
{
    if(item.tag != TOOLBAR_FIXED_SPACE_TAG && 
       item.tag != TOOLBAR_FLEXIBLE_SPACE_TAG)
    {
        //count real button:)
    }
}
0
rckehoe On

The answer above I wasn't able to get to actually work, so I used this. Hopefully this can help someone:

 for(int i=0; i<[buttonArray count]; i++){
      UIBarButtonItem *buttonItem = [[self items] objectAtIndex:i];
      if(buttonItem.title){
           NSLog(@"Double Boom %@", buttonItem);
      }
 }

** Flexible/Fixed space doesn't contain a title... This is the only real difference I could immediately see. So, I am literally just checking for a title.

0
Laszlo On

Dirty Swift 5.0 solution:

let fixedSpaces = toolbarItems?.filter({ $0.description.contains("systemItem=FixedSpace") })

It may break over time but will suffice for debug purposes.

Apply hand sanitizer after each use.