隐藏 UIToolbar UIBarButtonItems
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
重置项目:
您可以使用 items 属性,然后删除您不想显示的属性并传入新的
NSArray
。正如您所看到的,您还可以将其动画化以使用户清楚地了解。
Reset the items:
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.
我没有猜测索引,而是为 UIBarButtonItem 添加了一个 IBOutlet,然后按名称将其删除:
当然,它有助于连接设计器中的插座:)
Rather than guessing at the index, I added an IBOutlet for the UIBarButtonItem and then removed it by name:
And of course it helps to connect the outlets in the designer :)
我就是这样做的..太头疼了,但这是我能想到的最好的办法:
This is how i did it.. too much headache but its the best i could come up with :
我知道这对于那些寻找此页面寻求解决方案的人来说是相当古老的线程,在这里:
使用 iOS7,您可以使用此方法来显示/隐藏工具栏按钮:
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 :
这在这里不起作用,因为您使用 setItem 发送的数组不是函数所期望的。
我必须将这一行: 替换
为这一行:
然后它就完美地工作了。
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:
with this one:
Then it works perfectly.
Mohit 的答案是我用过的,但你不需要专门将其设置为工具栏设置的 NSArray。您可以将项目数组设置为 NSMutableArray。据我所知,没有真正的优势,但它的代码少了几行。这样,您就可以获取该数组并移动 UIButton 对象,就像处理任何其他带有对象的数组一样,然后只需使用该可变数组重置工具栏即可。
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.