Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIButton单击选定UIButton时高亮显示的状态不显示_Ios_Uibutton_Selected_Uicontrolevents_Touch Up Inside - Fatal编程技术网

Ios UIButton单击选定UIButton时高亮显示的状态不显示

Ios UIButton单击选定UIButton时高亮显示的状态不显示,ios,uibutton,selected,uicontrolevents,touch-up-inside,Ios,Uibutton,Selected,Uicontrolevents,Touch Up Inside,我希望我的UIButton在单击已选中的按钮时显示高亮显示的状态 基本上,在高亮显示状态下,我应用一个*.png图像作为我的UIButton背景图像,以提供按下的效果 但是如果当我再次点击按钮时,按钮已经处于选中状态,我就看不到高亮显示的状态,但它会直接进入正常状态 关注问题--> 请帮忙 //0 init UIButton UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, aSide, aSide)];

我希望我的UIButton在单击已选中的按钮时显示高亮显示的状态

基本上,在高亮显示状态下,我应用一个*.png图像作为我的UIButton背景图像,以提供按下的效果

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

关注问题-->

请帮忙

//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)
实际上都是不同的。如果要在(仅)高亮显示状态和高亮显示+选定状态下应用
阴影图像
,还必须设置:

[button setBackgroundImage:shadowImage forState:(UIControlStateHighlighted | UIControlStateSelected)]

swift
中,这将是:

button.setBackgroundImage(shadowImage, forState: UIControlState.Selected.union(UIControlState.Highlighted))
在Swift v3中(2016年11月):


Swift 4.2

仅以编程方式适用

aButton.setImage(UIImage(named: "your_image"), for: [.selected, .highlighted])

天哪!哈哈^我快疯了。当然谢谢你。。。我也尝试了这一行代码,但我删除了突出显示状态的行,因为我认为这是多余的:/IB中即使为选中并突出显示的状态设置了背景图像,当adjustsImageWhenHighlighted为YES(默认为YES)时,也会得到系统突出显示的图像(深灰色)或者普通图像。所以需要对Aaron Golden的答案进行编码。
aButton.setImage(UIImage(named: "your_image"), for: [.selected, .highlighted])