UIButton:设置选定突出显示状态的图像
我为按钮的状态“正常”、“突出显示”和“选定”设置了图像,但是当按钮处于选定状态并且我按下/突出显示它时,我没有看到突出显示的图像,而只是看到灰色的图片。 是否可以在选择按钮时设置突出显示状态的图像?
我的代码:
[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
当我这样做时:
[button setSelected:YES];
并按下按钮,“pressed.png”图像不会选择。
I set an images for button's states Normal,Highlighted and Selected, but when the button in selected state and I press/highlight it I didn't see my highlighted image but just grayed picture.
Is it possible to set an image for highlighted state when the button selected?
my code:
[button setImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"pressed.png"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateSelected];
when I do:
[button setSelected:YES];
and press the button, the "pressed.png" image doesn't select.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我找到了解决方案:需要添加附加行
I found the solution: need to add addition line
您可以在 Interface Builder 中执行此操作。
选择您想要在
IB
中设置的UIButton
,然后转到属性检查器
。在屏幕截图中,我使用的是自定义按钮类型,但这并不重要。
You can do this in Interface Builder.
Select the
UIButton
you wish to set inIB
then go to theattributes inspector
.In the screen shots,I am using a custom button type , but that does not matter.
Swift 3
要设置背景图像,我们可以使用
setBackgroundImage(_:for:)
Swift 2.x
Swift 3
To set the background image we can use
setBackgroundImage(_:for:)
Swift 2.x
我认为这里的大多数海报完全没有抓住要点。我也有同样的问题。最初的问题是关于选定按钮的突出显示状态(组合两个状态),该按钮无法在 IB 中设置,并且会回退到默认状态并发生一些变暗。正如一篇文章提到的,唯一可行的解决方案:
I think most posters here miss the point completely. I had the same problem. The original question was about the Highlighted state of a Selected button (COMBINING BOTH STATES) which cannot be set in IB and falls back to Default state with some darkening going on. Only working solution as one post mentioned:
就可以了:
如果你有充分的理由这样做,那么添加这些目标
If you have a good reason to do that, this will do the trick
add these targets:
在 Swift 3.x 中,您可以通过以下方式设置选择按钮时突出显示的图像:
In Swift 3.x, you can set highlighted image when button is selected in the following way:
很快你可以这样做:
In swift you can do:
就我而言,我必须将 UIButton.Type 从
.custom
更改为.system
并且:
处理点击时:
按钮。 isSelected = !button.isSelected
In my case, I have to change the UIButton.Type from
.custom
to.system
And:
When handling tapping:
button.isSelected = !button.isSelected
如果我错了请纠正我。通过这样做,
您可以清楚地更改所选按钮的状态。因此,很自然地,通过您提供的代码,图像将在您的案例中检查所选状态。png
Correct me if I am wrong. By doing
you are clearly changing the state of the buttons as selected. So naturally by the code you have provided the image will that for the selected state in your case checked.png
如果有人想知道这在 Swift 中是如何工作的,这是我的解决方案:
If someone's wondering how this works in Swift, here's my solution:
Swift 3+
OR
表示按钮当前处于<code>选中状态,然后你触摸它,显示
高亮
状态。Swift 3+
OR
It means that the button current in
selected
state, then you touch it, show thehighlight
state.我在设置
imageView.highlighted = NO;
时遇到问题(设置YES工作正常,图像更改为突出显示的图像)。
解决方案是调用
[imageView setHighlighted:NO];
一切正常。
I had problem setting
imageView.highlighted = NO;
(setting YES worked properly and the image changed to the highlighted one).
The solution was calling
[imageView setHighlighted:NO];
Everything worked properly.
您想从
方法中调用的位置:
上面的内容归功于 Jorge,但我对其进行了一些改进,提供了完整的工作解决方案
Where you want to call from
The method:
Credit above to Jorge but I improved it a bit giving the full working solution
Xamarin C#
执行按位 OR 由于某种原因不起作用
以下作品
Xamarin C#
Doing bitwise OR doesn't work for some reason
The following works
如果您还需要在按住所选状态的自定义按钮时操作系统默认提供的突出显示色调,请使用此 UIButton 子类。用 Swift 5 编写:
If you need the highlighted tint which the OS provides by default when you tap and hold on a custom button for the selected state as well, use this UIButton subclass. Written in Swift 5: