UIButton定制为使用标准图像/颜色突出显示状态

时间:2012-01-05 22:34:45

标签: iphone uibutton

对于视图中的一个UIButton,我创建了一个用于正常状态的图像,但我想继续使用默认的蓝色/图像作为突出显示/选择状态,有没有办法做到这一点?或者我是否真的必须为突出显示/选定的图像准备自定义图像?

以下是我在@mashios请求和建议下面的代码 -

UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
[shareButton setFrame:CGRectMake(1.0, 0.0, 99.0, 43.0)];
[shareButton setBackgroundImage:[[UIImage imageNamed:@"share_normal"] 
                                 stretchableImageWithLeftCapWidth:0.0 
                                 topCapHeight:0.0] forState:UIControlStateNormal];
[shareButton setBackgroundImage:[[UIImage imageNamed:@"share_normal"] // bottom_up_left_3_selected
                                 stretchableImageWithLeftCapWidth:0.0 
                                 topCapHeight:0.0] forState:UIControlStateHighlighted];
[shareButton addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];

2 个答案:

答案 0 :(得分:-1)

您可以将同一图像设置为两种状态。

答案 1 :(得分:-1)

首先,您想要正常,突出显示和选择状态的相同图像。在另一个评论中,您想要更改按钮的颜色。虽然我想建议你一些事情。 1.在将图像设置为按钮时。传递完整的图像名称字符串,如(something.jpg)。 2.使用stretchableImageWithLeftCapWidth& topCapHeight仅在需要时。 3.设置图像[[shareButton setBackgroundImage:[[UIImage imageNamed:@“share_normal.jpg / png”] forcontrolstate:UIControlStateNormal]; 我只是改变了这些东西,它对我有用。