ios – 单击选定UIButton时未显示UIButton突出显示状态

当我点击已经选中的按钮时,我希望我的UIButton显示突出显示的状态.

基本上在突出显示的状态下,我将* .png图像应用为我的UIButton backgroundImage以产生按下效果.

但是如果按钮已经处于选中状态当我再次点击它时,我只是看不到突出显示的状态,但它直接进入正常状态!

观看问题 – > Video of the Issue!

请帮忙

//0    init UIButton
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x,y,aSide,aSide)];

//1    Give it a backgroundColor
[button setBackgroundColor:aColor];

[..]

//2    Set titleLabel and its style
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];

UIImage *shadowImage = [UIImage imageNamed:kBtnShadow];
shadowImage = [shadowImage stretchableImageWithLeftCapWidth:floorf(shadowImage.size.width/2) topCapHeight:floorf(shadowImage.size.height/2)];

[button setBackgroundImage:shadowImage forState: UIControlStateHighlighted];

[button setTitle:aLabel forState:  UIControlStateNormal];

//3    Assign tag and Action
[button setTag:tag];
[button addTarget:target action:a forControlEvents:UIControlEventTouchUpInside];

解决方法

各种状态:UIControlStateNormal,UIControlStateSelected和(UIControlStateSelected | UIControlStateHighlighted)实际上都是不同的.如果您希望shadowImage同时应用于(仅)突出显示的状态和突出显示的选定状态,则还必须设置:
[button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)]

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...