简体   繁体   English

如何测试工具栏中的项目是否为 FlexibleSpace 项目?

[英]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:由于A-Live证实了我的发现,即无法查询 UIBarButtonItem 以检查它是否是 FlexibleSpace(或 FixedSpace),我使用标签将这些项目标记为灵活和固定空间(2 个不同的整数)并将这些数字放入常量然后在我使用的代码中:

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: Dirty Swift 5.0 解决方案:

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.每次使用后都要使用洗手液。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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