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

前端之家收集整理的这篇文章主要介绍了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)]

猜你在找的iOS相关文章