简体   繁体   English

如何更改iPhone上按钮的默认颜色?

[英]How can I change the default color of the button on iPhone?

如何更改iPhone上按钮的默认颜色?

A common technique is to use an image for the background. 一种常见的技术是使用图像作为背景。 Actually you can use a stretchable image: 实际上你可以使用一个可伸缩的图像:

- (void)viewDidLoad {
    UIImage *blueImage = [UIImage imageNamed:@"blueButton.png"];
    UIImage *blueButtonImage = [blueImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [myButton setBackgroundImage:blueButtonImage forState:UIControlStateHighlighted];
}

The original image: 原始图片:

替代文字

The end result: 最终结果:

替代文字

I came across this post that explains how to do this without images and completely in code. 我发现这篇文章解释了如何在没有图像和完全代码的情况下完成此操作。

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/ http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

Hope that helps. 希望有所帮助。

Assuming you mean UIButton , you can only change the title color using setTitleColor:forState: . 假设您的意思是UIButton ,您只能使用setTitleColor:forState:更改标题颜色setTitleColor:forState:

You can't change the background color or the shape. 您无法更改背景颜色或形状。 You have to create your own control for that. 你必须为此创建自己的控件。

I started this thread on iphonedevsdk.com. 我在iphonedevsdk.com上开始了这个帖子。

Hopefully we'll get an answer there if not here. 希望如果不在这里,我们会得到答案。

A great way to customize UIButtons is by directly manipulating the Core Animation Layers. 自定义UIButtons的一个好方法是直接操作核心动画层。 You can find a good tutorial on "Cocoa is My Girlfriend" 你可以找到关于“可可是我的女朋友”的好教程

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/ http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM