单击 Xcode 4.3.3 时保持 A 按钮突出显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11607092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep A button highlighted when clicked Xcode 4.3.3
提问by Elias Rahme
I have an application that needs somewhere a button to stay highlighted when pressed. This button should stay highlighted when pressed to inform the users that the specific page is added to favorites. And can be unpressed. Any ideas how to do it?
我有一个应用程序需要某个按钮在按下时保持突出显示。按下此按钮时应保持突出显示,以通知用户特定页面已添加到收藏夹。并且可以解压。任何想法如何做到?
采纳答案by mayuur
Yes, just keep an image for the Button for Highlighted state like this.
是的,只需像这样为突出显示状态的按钮保留一个图像。
[myBtn setImage:@"highlightedImage.png" forState:UIControlStateHighlighted];
Now, in the method for button check for highlighted version and normal version
现在,在按钮检查高亮版本和普通版本的方法中
-(IBAction) startWorkoutClicked:(id)sender
{
if(btnStart.highlighted)
{
btnStart.highlighted = NO; //btn changes to normal state
}
else
{
btnStart.highlighted = YES; //btn changes to highlighted state
}
}
回答by manileo86
[yourButton setImage:[UIImage imageNamed:@"normalState.png"] forState:UIControlStateNormal];
[yourButton setImage:[UIImage imageNamed:@"highlightedState.png"] forState:UIControlStateSelected];
[yourButton setImage:[UIImage imageNamed:@"highlightedState.png"] forState:UIControlStateSHighlighted];
Set this image preferences and whenever you want call
设置此图像首选项并在您需要时随时调用
[yourButton setSelected:YES];
which will change your button to highlighted state. You can change it to normal by doing
这会将您的按钮更改为突出显示状态。您可以通过执行将其更改为正常
[yourButton setSelected:NO];
Hope this helps!
希望这可以帮助!
回答by John Spencer
All is good with the above answer , apart from the typo, which could lead to problems.. UIControlStateSHighlighted should be UIControlStateHighlighted
除了可能导致问题的错字外,上述答案一切都很好.. UIControlStateSHighlighted 应该是 UIControlStateHighlighted
there are more examples here
这里有更多例子