禁用的按钮不会更改图像 [英] Disabled buttons not changing image in
问题描述
我有一个按钮可以按下设置viewController
.在该设置viewController
上,我有一个开关可以反转原始视图上的所有颜色,这是我以编程方式完成的.我制作了反转按钮图像以替换原始图像.
当我返回原始视图时,如果开关已被翻转,我已经viewWillAppear
调用该方法来反转.除了两个禁用的按钮之外,其他所有内容都会相应地发生变化.
开关值保存在默认设置下,以便用户可以退出应用程序并稍后再返回,并且仍然具有反转的颜色.当viewDidLoad
调用invert方法时,按钮的变化就很好,它们甚至显示为调整状态以表明它们已被禁用.
有什么想法吗?
-(void)invert{
self.view.backgroundColor = [UIColor blackColor];
//Buttons
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
forState:UIControlStateNormal];
}
-(void)viewWillAppear:(BOOL)animated{
_inverted =
[[NSUserDefaults standardUserDefaults] boolForKey:@"isInvertedOn"];
[[NSUserDefaults standardUserDefaults] synchronize];
if(_inverted){
[self invert];
} else {
[self unInvert];
}
[super viewWillAppear:(BOOL)animated];
}
问题似乎是图像没有自动更新,您需要重置enabled
值才能使它起作用:
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
forState:UIControlStateNormal];
self.stopButton.enabled = ! self.stopButton.enabled;
self.stopButton.enabled = ! self.stopButton.enabled;
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
forState:UIControlStateNormal];
self.playButton.enabled = ! self.playButton.enabled;
self.playButton.enabled = ! self.playButton.enabled;
I have a button that pushes to a settings viewController
. On that settings viewController
I has a switch to invert all the colors on the original view, an inversion which I've done programmatically. I made inverted button images to replace the original images.
When I return to the original view, I have viewWillAppear
call the method to invert if the switch has been flipped. Everything changes accordingly except for two disabled buttons.
The switch value is saved under the default settings so that the user can exit the app and come back later and still have the colors inverted. When viewDidLoad
calls the invert method, the buttons change just fine, and they even show up adjusted to show they are disabled.
Any idea what might be going on?
-(void)invert{
self.view.backgroundColor = [UIColor blackColor];
//Buttons
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
forState:UIControlStateNormal];
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
forState:UIControlStateNormal];
}
-(void)viewWillAppear:(BOOL)animated{
_inverted =
[[NSUserDefaults standardUserDefaults] boolForKey:@"isInvertedOn"];
[[NSUserDefaults standardUserDefaults] synchronize];
if(_inverted){
[self invert];
} else {
[self unInvert];
}
[super viewWillAppear:(BOOL)animated];
}
The issue seems to be that the image is not updated automatically, you'll need to reset the enabled
value to make it work:
[self.stopButton setImage:[UIImage imageNamed:@"stopinverted"]
forState:UIControlStateNormal];
self.stopButton.enabled = ! self.stopButton.enabled;
self.stopButton.enabled = ! self.stopButton.enabled;
[self.playButton setImage:[UIImage imageNamed:@"playinverted"]
forState:UIControlStateNormal];
self.playButton.enabled = ! self.playButton.enabled;
self.playButton.enabled = ! self.playButton.enabled;
这篇关于禁用的按钮不会更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!