我有一张480x331的图像并且背景透明。透明部分就在图像的边缘。我的UIButton是72x37。当我在IB的按钮中将图像设置为背景图像时,图像的透明边缘在按钮中显示为白色。没有图像,按钮是白色的圆角。我猜这是我看到的白色。如果是这样,则表示图像透明度正常,但按钮的白色区域正在显示。

如果我将图像设置为UIButton的Image属性,即使在更改视图模式后,它也无法正确缩放,并保持其完整大小。

我应该使用哪些属性来获得扩展和透明度才能正常工作?

有帮助吗?

解决方案

您使用的按钮类型是什么?

通过它的声音你可能使用UIButtonTypeRoundedRect样式,但是UIButtonTypeCustom可能更合适。您可以在Interface Builder的检查器窗口中更改它。

其他提示

@Christopher Fairbaim建议使用UIButtonTypeCustom是正确的。使用UIButtonTypeRoundedRect将为您提供透明区域中的白色背景。

但是,在Interface Builder中进行连接有其局限性。如果您想从数据集或基于用户输入动态生成UIViews,UIButtons和资源,那么以编程方式执行此操作即可。

要创建按钮:(注意:这也适用于其他UIButtonTypes,但我已将UIButtonTypeCustom用于此特定透明示例。)

UIButton *btnName = [UIButton buttonWithType:UIButtonTypeCustom];

要更改所需的属性:

btnName.backgroundColor = [UIColor clearColor];
btnName.frame = CGRectMake(100, 100, 72, 37);  //(X coord, Y coord, width, height)
[btnName addTarget:myActionClassName action:@selector(myActionName) forControlEvents:UIControlEventTouchUpInside];

为各种状态添加图像(假设它们是透明的.png):

UIImage *btnNameImageNormal = [UIImage imageNamed:@"btnNameNormal.png"];
[btnName setBackgroundImage:btnMenuImageNormal forState:UIControlStateNormal];
UIImage *btnNameImagePressed = [UIImage imageNamed:@"btnNamePressed.png"];
[btnName setBackgroundImage:btnNameImagePressed forState:UIControlStateHighlighted];

要将视图添加到视图中(来自viewController类):

[self addSubview:btnName];

(请原谅任何错别字,因为我从我的一个项目中复制并粘贴,并试图概括命名。)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top