隐藏 UIToolbar UIBarButtonItems

发布于 2024-10-17 07:06:51 字数 168 浏览 6 评论 0原文

我有一个 UIToolbar,我使用 IB 设置了三个按钮:左、中、右。在某些情况下,我不想显示中间按钮。有谁知道如何隐藏 UIToolBar 内的特定按钮?没有隐藏属性,我所能找到的只是 setEnable 但这仍然留下了按钮,导致用户想知道它的用途是什么。我只想在它确实有用的情况下显示它。

提前致谢!

I have a UIToolbar that I set up using IB with three buttons, left, middle and right. In some situations I would like to not display the middle button. Does anybody know of a way to hide a specific button on inside a UIToolBar? There is no hide property, all I can find is setEnable but this still leaves the button causing users to wonder what its purpose is. I would like to only display it in situations that it actually has a use.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疯到世界奔溃 2024-10-24 07:06:51

重置项目

-(void)setItems:(NSArray *)items animated:(BOOL)animated

您可以使用 items 属性,然后删除您不想显示的属性并传入新的 NSArray

正如您所看到的,您还可以将其动画化以使用户清楚地了解。

Reset the items:

-(void)setItems:(NSArray *)items animated:(BOOL)animated

You can get the current items using the items property, then just remove the one you don't want to show and pass in the new NSArray.

As you can see, you can also animate it to make it clear to the user.

睡美人的小仙女 2024-10-24 07:06:51

我没有猜测索引,而是为 UIBarButtonItem 添加了一个 IBOutlet,然后按名称将其删除:

NSMutableArray *toolBarButtons = [self._toolbar.items mutableCopy];
[toolBarButtons removeObject:self._selectButton]; // right button
[self._toolbar setItems:toolBarButtons];

当然,它有助于连接设计器中的插座:)

Rather than guessing at the index, I added an IBOutlet for the UIBarButtonItem and then removed it by name:

NSMutableArray *toolBarButtons = [self._toolbar.items mutableCopy];
[toolBarButtons removeObject:self._selectButton]; // right button
[self._toolbar setItems:toolBarButtons];

And of course it helps to connect the outlets in the designer :)

春庭雪 2024-10-24 07:06:51

我就是这样做的..太头疼了,但这是我能想到的最好的办法:

NSArray *toolBarArray = toolBar.items;
NSMutableArray *newToolBarArray = [NSMutableArray arrayWithArray:toolBarArray];
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
//remove whatever buttons you want to.

NSArray *finalTabBarArray =[[NSArray alloc] initWithObjects:newToolBarArray, nil];
[toolBar setItems:[finalTabBarArray objectAtIndex:0] animated:NO];

This is how i did it.. too much headache but its the best i could come up with :

NSArray *toolBarArray = toolBar.items;
NSMutableArray *newToolBarArray = [NSMutableArray arrayWithArray:toolBarArray];
[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
//remove whatever buttons you want to.

NSArray *finalTabBarArray =[[NSArray alloc] initWithObjects:newToolBarArray, nil];
[toolBar setItems:[finalTabBarArray objectAtIndex:0] animated:NO];
怂人 2024-10-24 07:06:51

我知道这对于那些寻找此页面寻求解决方案的人来说是相当古老的线程,在这里:

使用 iOS7,您可以使用此方法来显示/隐藏工具栏按钮:

    if(// your code Condition) 
{ self.toolbarBtn1.enabled = YES;
 self.toolbarBtn1.tintColor = nil; }
 else
 { self.toolbarBtn1.enabled = NO; 
self.toolbarBtn1.tintColor = [UIColor clearColor]; }

I know it is quite old thread for but those who look this page for solution, here you go :

With iOS7, you can use this approach to show/hide your toolbar button :

    if(// your code Condition) 
{ self.toolbarBtn1.enabled = YES;
 self.toolbarBtn1.tintColor = nil; }
 else
 { self.toolbarBtn1.enabled = NO; 
self.toolbarBtn1.tintColor = [UIColor clearColor]; }

心房敞 2024-10-24 07:06:51

这在这里不起作用,因为您使用 setItem 发送的数组不是函数所期望的。

我必须将这一行: 替换

NSArray *finalTabBarArray = [[NSArray alloc] initWithObjects:newToolBarArray, nil];

为这一行:

NSArray *finalTabBarArray = [newToolBarArray copy];

然后它就完美地工作了。

This does not work here because the array you are sending with setItem is not what the function expects.

I had to replace the line:

NSArray *finalTabBarArray = [[NSArray alloc] initWithObjects:newToolBarArray, nil];

with this one:

NSArray *finalTabBarArray = [newToolBarArray copy];

Then it works perfectly.

独留℉清风醉 2024-10-24 07:06:51

Mohit 的答案是我用过的,但你不需要专门将其设置为工具栏设置的 NSArray。您可以将项目数组设置为 NSMutableArray。据我所知,没有真正的优势,但它的代码少了几行。这样,您就可以获取该数组并移动 UIButton 对象,就像处理任何其他带有对象的数组一样,然后只需使用该可变数组重置工具栏即可。

[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
[toolBar setItems:newToolBarArray];

Mohit's answer is one that I have used, but you dont need to specifically make it a NSArray that the toolbar sets. You can just set the array of items as a NSMutableArray. No real advantage that I am aware off but its a few lines less code. And that way you can take the array and move about UIButton objects as you would any other array with objects and then just reset the toolbar with that mutable array.

[newToolBarArray removeObjectAtIndex:2];
[newToolBarArray removeObjectAtIndex:1];
[toolBar setItems:newToolBarArray];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文