ios UIButton 选择颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6066776/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:01:37  来源:igfitidea点击:

UIButton selection color

iphoneobjective-ccocoa-touchiosuibutton

提问by Abhinav

I am drawing custom a UIButton and want to set the blue color when it is highlighted. How can I achieve this?

我正在绘制自定义 UIButton 并希望在突出显示时设置蓝色。我怎样才能做到这一点?

UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:[[UIImage imageNamed:@"test.png"] stretchableImageWithLeftCapWidth:100 topCapHeight:0] forState:UIControlStateNormal];

采纳答案by Sola

You can try to use another image to do the job. You can also refer to this link:

您可以尝试使用另一个图像来完成这项工作。你也可以参考这个链接:

Is it even possible to change a UIButtons background color?

甚至可以更改 UIButtons 的背景颜色吗?

回答by MobileSoul

You can do following to set images for your state (normal/highlight/selected). You have to have images.

您可以执行以下操作来为您的状态(正常/突出显示/选定)设置图像。你必须有图像。

[downButton setImage:[UIImage imageNamed:@"white.png"] forState:UIControlStateNormal];
[downButton setImage:[UIImage imageNamed:@"blue.png"] forState:UIControlStateHighlighted];

回答by Chris Allinson

Ya, you can create a 1px by 1px "blue colored" image and use the following:

是的,您可以创建一个 1px x 1px 的“蓝色”图像并使用以下内容:

[yourButton setBackgroundImage:[UIImage imageNamed:@"theImageYouMade.png"] forState:UIControlStateHighlighted];

You can't use "setImage:" in my method because the highlighted state of the button will actually just display the 1px by 1px image in the center of the button.

您不能在我的方法中使用“setImage:”,因为按钮的突出显示状态实际上只会在按钮中心显示 1px x 1px 的图像。

My method works for variable sized buttons. Using setImage: requires you to make images that are exactly the same size as your button.

我的方法适用于可变大小的按钮。使用 setImage: 要求您制作与按钮大小完全相同的图像。

-Chris

-克里斯

回答by Hot Licks

Maybe set a different background image for a different state?

也许为不同的状态设置不同的背景图像?

回答by matt

Provide a background image for UIControlStateHighlighted. Also it might help to turn off adjustsImageWhenHighlighted, though this should not matter if you're providing a separate background image for the highlighted state.

为 提供背景图像UIControlStateHighlighted。此外,关闭 可能会有所帮助adjustsImageWhenHighlighted,但如果您为突出显示的状态提供单独的背景图像,这应该无关紧要。