iOS - Setting UIButton background color only color

2019-02-11 23:19发布

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?

3条回答
我只想做你的唯一
2楼-- · 2019-02-11 23:59

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

查看更多
疯言疯语
3楼-- · 2019-02-11 23:59

I discovered this almost by accident:

self.loginButton.tintColor = [UIColor redColor];

Definitely works and has been available since iOS 2.0.

查看更多
走好不送
4楼-- · 2019-02-12 00:17

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 here which can give you any-sized gradient type buttons using layers, no image files required.

查看更多
登录 后发表回答