简体   繁体   中英

How to test if item in toolBar is FlexibleSpace item?

我想知道UIBarButtonItem枚举的self.toolbarItems是按钮,哪个是灵活的空间项。

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:)
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM