【发布时间】:2011-06-02 06:06:36
【问题描述】:
我有一个透明图像(“hold_empty.png”,下面),我想把它变成一个按钮。在按钮内部会有一个带有彩色背景的视图,该视图的尺寸比实际图像略小,以保持背景颜色。
这是因为图片有圆角,所以我不能简单地在图片上加上背景颜色,因为它看起来比图片大。
图像是一个带有“圆角”的透明正方形。我试图创建的效果应该像图层。
- 背景颜色层(红色、绿色等)
- “hold_empty.png”图片(上图)
- 整个对象(包括背景颜色层)应该是可点击的。
我遇到的问题是只有图像(及其边框)似乎是可点击的,而不是整个对象。
代码如下。
// x,y,w,h
CGRect frame = CGRectMake(10, 10, 72, 72);
CGRect frame2 = CGRectMake(5, 5, 60, 60); // I know this is not filling the image, its just a test
UIView *bgColorView = [[UIView alloc] initWithFrame:frame2];
[bgColorView setFrame:frame2];
bgColorView.backgroundColor = [UIColor redColor];
UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hold_empty" ofType:@"png"]];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addSubview:bgColorView];
[btn setImage:image forState:UIControlStateNormal];
btn.frame = frame;
[btn addTarget:self action:@selector(btnSelectColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[bgColorView release];
所以,总结一下:如何制作具有可点击背景颜色的透明图像按钮?
谢谢。
【问题讨论】:
标签: iphone uiimage uibutton transparency background-color