gpt4 book ai didi

iphone - 在 UIBarButtonItem 中切换自定义图像

转载 作者:行者123 更新时间:2023-12-03 20:35:59 25 4
gpt4 key购买 nike

在过去的两天里,我一直在绞尽脑汁地寻找并尝试一些我自己的解决方案。我通过 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"];
}

最佳答案

我终于弄清楚了......只花了几天时间,但我太忙了,没有时间发布解决方案。我终于有时间了,很高兴发布我的解决方案。我有预感,除非 100% 以编程方式完成,否则这是行不通的,我是对的。这是我的问题的最终解决方案:

if(mute == YES)
{
UIImage *image = [UIImage imageNamed:@"audio-off.png"];
UIButton *myMuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
myMuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myMuteButton setImage:image forState:UIControlStateNormal];
[myMuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myMuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myMuteButton];
navBar.leftBarButtonItem = myMuteBarButtonItem;
[myMuteBarButtonItem release];
}
else
{
UIImage *image = [UIImage imageNamed:@"audio-on.png"];
UIButton *myUnmuteButton = [UIButton buttonWithType:UIButtonTypeCustom];
myUnmuteButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myUnmuteButton setImage:image forState:UIControlStateNormal];
[myUnmuteButton addTarget:self action:@selector(mute) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *myUnmuteBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myUnmuteButton];
navBar.leftBarButtonItem = myUnmuteBarButtonItem;
[myUnmuteBarButtonItem release];
}

好消息是我终于完成了我的应用程序并将其提交到应用程序商店。希望一切顺利,我很期待!

关于iphone - 在 UIBarButtonItem 中切换自定义图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3117971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com