iOS - 设置 UIButton 背景颜色只为角落着色

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

iOS - Setting UIButton background color only colors the corners

iosuibuttonuicolorcgcolor

提问by 8vius

I'm trying to customize my button by changing their colors, but when I use either:

我正在尝试通过更改它们的颜色来自定义我的按钮,但是当我使用以下任一方法时:

self.loginButton.layer.backgroundColor = [UIColor colorWithRed:0.0 green:157.0/255.0 blue:223.0/255.0 alpha:1.0].CGColor;

or

或者

self.loginButton.backgroundColor = [UIColor colorWithRed:0.0 green:157.0/255.0 blue:223.0/255.0 alpha:1.0];

All I get is this result:

我得到的只是这个结果:

enter image description here

在此处输入图片说明

What I want to do is change the white color to blue, any idea what I'm doing wrong?

我想要做的是将白色更改为蓝色,知道我做错了什么吗?

回答by jrturton

You're using a round rect button which is pretty ugly, and pretty hard to customise.

您正在使用一个圆形的矩形按钮,它非常难看,而且很难定制。

You can create a custom type button and use a background image, or create a custom type button and round the corners and set the background colour of the layer yourself.

您可以创建自定义类型按钮并使用背景图像,或创建自定义类型按钮并圆角并自己设置图层的背景颜色。

If you want to get really fancy there is a nice tutorial herewhich can give you any-sized gradient type buttons using layers, no image files required.

如果你想变得非常喜欢这里有一个很好的教程它可以为你提供使用图层的任何大小的渐变类型按钮,不需要图像文件。

回答by Dongsheng Cai

What's that button type? If you want to customise button, you gotta create a button with [UIButton buttonWithType:UIButtonTypeCustom]

那个按钮类型是什么?如果你想自定义按钮,你必须用 [UIButton buttonWithType:UIButtonTypeCustom] 创建一个按钮

回答by mpemburn

I discovered this almost by accident:

我几乎是偶然发现的:

self.loginButton.tintColor = [UIColor redColor];

Definitely works and has been available since iOS 2.0.

绝对有效并且自 iOS 2.0 以来一直可用。