Ios7 启用/禁用状态下带有tintColor的UIButton的怪异行为
更新: 即使是Ios7 启用/禁用状态下带有tintColor的UIButton的怪异行为,ios7,uibutton,tintcolor,Ios7,Uibutton,Tintcolor,更新: 即使是uibarbuttonims也不会在视觉上响应状态变化 场景: 我有一个uibuttonypesystem类型的uibuttonypesystem初始化如下: sendButton = [UIButton buttonWithType:UIButtonTypeSystem]; sendButton.backgroundColor = [UIColor clearColor]; [sendButton setTintColor:UIColorFromRGB(SEND_BUTTON
uibarbuttonim
s也不会在视觉上响应状态变化
场景:
我有一个uibuttonypesystem
类型的uibuttonypesystem
初始化如下:
sendButton = [UIButton buttonWithType:UIButtonTypeSystem];
sendButton.backgroundColor = [UIColor clearColor];
[sendButton setTintColor:UIColorFromRGB(SEND_BUTTON_COLOR)];
sendButton.opaque = YES;
sendButton.clearsContextBeforeDrawing = NO;
sendButton.frame = CGRectMake(275, 6, 50, 35);
UIImage* sendImage = [UIImage imageNamed:@"toilet_paper"];
[sendButton setImage:[UIImage imageWithCGImage:sendImage.CGImage scale:sendImage.scale orientation:UIImageOrientationLeft]
forState:UIControlStateNormal];
sendButton.enabled = NO;
[sendButton addTarget:self action:@selector(post) forControlEvents:UIControlEventTouchUpInside];
目的:
它与UITextView
关联,这样,如果textView
中有一些文本,并且我的主机可用(通过可达性检查),并且在textViewDidChange:
委托方法中更改了它的enabled
属性,则将其设置为enabled
sendButton.enabled = [APP_DELEGATE hostAvailable] && [myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n"]].length > 0;
根据此已启用
状态,按钮必须在发送按钮颜色
(已启用
=是)和灰色
(已启用
=否)之间切换
问题:
到目前为止,该代码一直运行良好。当文本视图中没有文本时,它是灰色的,当文本视图中有文本时,它就变成了SEND\u BUTTON\u COLOR
。然而,它突然停止了这种行为。不管文本视图的内容如何,它始终保持灰色。一旦按下,它将变为SEND_BUTTON_COLOR
,并保持这种状态,再次与文本视图文本无关
如何恢复我以前在
ui按钮上使用的按钮的行为?您必须在textViewDidChange委托方法中再次设置按钮的着色颜色
在要设置启用状态的代理中:
sendButton.enabled = [APP_DELEGATE hostAvailable] && [myTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n"]].length > 0;
if(sendButton.enabled){
[sendButton setTintColor:UIColorFromRGB(SEND_BUTTON_COLOR)];
}
else{
//Set Another Color For Disabled State
}
看来问题出在我身上。我在另一个VC的.m文件中对UIButton
进行了子类化(不是由相关VC或其导入的任何其他VC导入的):
不知怎的,这干扰了我的按钮。经过数周的进一步子类化、更改图像、更改标题等工作后,我能够通过跳转到启用了的方法的定义(CMD
+单击
)来诊断问题(无意中),并直接找到问题的根源(子类代码)。评论出来了,瞧。一切都恢复正常了
我希望这对将来面临同样问题(或犯同样错误)的人有所帮助。启用的状态是否正在改变?或者只是tintColor
?启用的状态会改变。只有tintColor
没有响应。我删除了最近为更改titleTextAttributes
而添加的UIAppearance
代码,但没有解决问题。能否显示切换颜色的代码?我没有切换颜色。我正在切换启用
状态。这都是问题。对不起,我不明白。当已启用
状态更改时,如何更改颜色?它是按钮上的图像,而不是文本。如果是这样的话,事情就容易多了。我问的是ui按钮的默认tintColor
行为,这取决于它的启用状态。
@implementation UIButton (Border)
- (void) setEnabled:(BOOL)enabled {
if (enabled) {
self.layer.borderColor = UIColorFromRGB(0x888888).CGColor;
} else {
self.layer.borderColor = UIColorFromRGB(0xdddddd).CGColor;
}
[super setEnabled:enabled];
[self setNeedsDisplay];
}
@end