问题描述
我希望我的 UIButton 在我单击已选择的按钮时显示突出显示状态.
I want my UIButton to show up the highlighted state when I click over a button that is already selected.
基本上在突出显示的状态下,我将 *.png 图像应用为我的 UIButton backgroundImage 以提供按下效果.
Basically in the highlighted state I apply a *.png image as my UIButton backgroundImage to give a pressed down effect.
但是如果按钮已经处于选中状态,当我再次点击它时,我只是看不到突出显示的状态,但它会直接进入正常状态!
But if the button is already in the Selected State When I click over it again I just can't see the highlighted state but it goes straight to the normal state!
观看问题--> 问题视频!
请帮忙
//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
同时应用于(仅)突出显示状态和突出显示+选择状态,您还必须设置:
The various states: UIControlStateNormal
, UIControlStateSelected
, and (UIControlStateSelected | UIControlStateHighlighted)
are all actually distinct. If you want your shadowImage
to apply both in the (only) highlighted state and in the highlighted+selected state, you must also set:
[button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)]
这篇关于单击选定的 UIButton 时未显示 UIButton 突出显示状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!