在 UIBarButtonItem 中切换自定义图像
在过去的两天里,我一直在绞尽脑汁地寻找并尝试一些自己的解决方案。我通过 IB 放置了一个 UIBarButtonItem,并在顶部栏中放置了一个图像,作为静音/取消静音按钮。除了图像不变之外,一切正常。我使用了以下代码,它可以编译但没有改变
if( mute == YES ) {
UIImage *unmuteImage = [UIImage imageNamed:@"audio-on.png"];
[self.muteButton setImage:unmuteImage];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"muteKey"];
}
else {
UIImage *muteImage = [UIImage imageNamed:@"audio-off.png"];
[self.muteButton setImage:muteImage];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"muteKey"];
}
I've been breaking my head for the past two days searching and trying some of my own solutions. I placed a UIBarButtonItem through IB with an image in the top bar to act as a mute/unmute button . Everything works except the image doesn't change. I used the following code and it compiles but no change
if( mute == YES ) {
UIImage *unmuteImage = [UIImage imageNamed:@"audio-on.png"];
[self.muteButton setImage:unmuteImage];
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"muteKey"];
}
else {
UIImage *muteImage = [UIImage imageNamed:@"audio-off.png"];
[self.muteButton setImage:muteImage];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"muteKey"];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于弄清楚了......只花了几天时间,但我太忙了,无法发布解决方案。我终于有时间了,很高兴发布我的解决方案。我有预感,除非 100% 以编程方式完成,否则这是行不通的,我是对的。这是我的问题的最终解决方案:
好消息是我终于完成了我的应用程序并将其提交到应用程序商店。希望一切顺利,我很期待!
I finally figured it out...only took a couple of days but I've been too busy to post up a solution. We'll I finally got time and am happy to post my solution. I had a hunch that this would'nt work unless it was done 100% programmatically, and I was right. Here's the final solution to my problem:
the good news is I finally finished my app and submitted it to the app store. Hopefully everything will go smooth and am looking forward to it!
Swift,
我围绕实例变量设置了我的变量,并用它来切换我的开关。我的导航栏中也有 3 个按钮。
Swift,
I set mine around an instance var, and used that to toggle my switch. I also had 3 buttons in my nav bar.