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];

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

首先,您想要相同的图像用于正常、高亮显示和选定状态。在另一条评论中,您想更改按钮的颜色。虽然我只想向你建议几件事。1.请在将图像设置为按钮时。传递完整的图像名称字符串,如(something.jpg)。2.使用具有LeftCapWidth&topCapHeight(仅在需要时)。3.将图像设置为[[shareButton setBackgroundImage:[[UIImage imageNamed:@"share_normal.jpg/png"]forcontrolstate:UIControlStateNormal];4.我只是改变了这些东西,它对我很有效。

最新更新