以编程方式更改(而不是初始化)UIBarButtonItem 标识符?
在 IB 中,我可以将 UIBarButtonItem 的标识符设置为“play”,这会添加播放按钮的图像(右指三角形)。
有没有办法以编程方式更改此图像?我想在按下播放按钮时将其更改为“暂停”。
我知道您可以使用标识符初始化 UIBarButtonItem,但我还没有找到在初始化后更改它的方法。这可能吗?
我唯一能想到的就是删除旧按钮并在其位置初始化一个新按钮,但这似乎效率很低。
有什么想法吗?
In IB I can set the identifier of a UIBarButtonItem to 'play' which adds an image of a play button (right-pointing triangle).
Is there a way to change this image programmatically? I want to change it to 'pause' when the play button is pressed.
I know you can initialize a UIBarButtonItem with an identifier but I've yet to find a way to change it after it's been initialized. Is this even possible?
The only thing I can think of is to remove the old button and initialize a new one in its place, but this hardly seems efficient.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,我已经用谷歌搜索了这个问题,并遇到了 来自的示例代码Apple 他们做完全相同的事情(在工具栏按钮上切换播放/暂停按钮图形)。但他们没有使用
UIBarButtonItem
的内置播放和暂停标识符,而是使用自定义UIButton
并切换自定义图像。因此,如果苹果在 UIButton 上创建和切换自定义图像而不是内置的播放和暂停
UIBarButtonItem
按钮上遇到麻烦,那么我认为可以肯定地说,没有办法以编程方式更改一个 UIBarButtonItem。这就是他们(Apple)在按下按钮时切换图像的方法:
将 p.playing 替换为您想要保持按钮状态的任何 BOOL 。
playButton
是工具栏中的自定义UIButton
。pauseBtnBG
和playBtnBG
是要切换的图像。Ok I've googled this question to death and ran into sample code from Apple where they do exactly the same thing (toggle play/pause button graphic on a toolbar button). But instead of using the built in play and pause identifiers of
UIBarButtonItem
they use a customUIButton
and toggle custom images.So if Apple goes through the trouble of creating and toggling custom images on a UIButton instead of the built in play and pause
UIBarButtonItem
buttons then I think it's pretty safe to say there's no way to programatically change the identifier of aUIBarButtonItem
.This is what they (Apple) do to toggle the images when the button is pressed:
Replace p.playing with whatever BOOL you want to hold the state of your button.
playButton
is the customUIButton
in the toolbar.pauseBtnBG
andplayBtnBG
are the images to toggle.这似乎工作得相当好:
在这个例子中,我有一个 UIWebView 的工具栏,当有人单击“重新加载”时,我希望它更改为“停止”。工具栏上只有一个灵活的空间和一个按钮 - 右对齐按钮 - 所以我抓住了对旧按钮的引用,使用与旧按钮相同的选择器创建了一个新按钮,重置选项卡栏上的按钮,然后松开原来的按钮。
不是最漂亮的,但您可以使用所有标准按钮,而无需覆盖按钮类。
This seems to work fairly well:
In this example I had a toolbar for a UIWebView and when someone clicked Reload I wanted it to change to Stop. The toolbar only had a flexible space and the one button on it - to right-align the button - so I grabbed a reference to the old button, made a new one with the same selector as the old, reset the buttons on the tab bar, and then released the original button.
Not the prettiest, but you can use all standard buttons without having to override the button class(es).
两个堆叠的工具栏怎么样?然后,您可以将一些系统按钮放在顶部,将其他按钮放在底部。如果按下播放按钮,则只需隐藏顶部工具栏即可。
What about 2 stacked toolbars? Then you can have some system buttons in the top one, and others in the bottom one. If the play button is pressed, then just hide the top toolbar.