2

我尝试UIButton在文本上方制作一个带有图像的图像,所以我使用以下代码:

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(x, y, 140, 140);
[btn setTitle:self.objMateria.titoloMateria forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setImage:[UIImage imageNamed:@"pala.png"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 20, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, 0, 0, 0);

问题是这段代码我看不到文字,似乎被图像覆盖了,错误在哪里?

这里的图像:
在此处输入图像描述

4

2 回答 2

3

试试这个

btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, -120, 0, 0);

调整-120ie left的值以水平适合您的标题

于 2012-12-17T14:50:57.450 回答
1

您可以按如下方式修改您的代码吗?

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 20, 140, 140);
[btn setTitle:@"pala" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor]];
[btn setImage:[UIImage imageNamed:@"pala.png"] forState:UIControlStateNormal];
btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
UIImage *imageTemp = [UIImage imageNamed:@"pala.png"];
btn.titleEdgeInsets = UIEdgeInsetsMake(imageTemp.size.height, -imageTemp.size.width, 0, 0);
[self.view addSubview:btn];
于 2012-12-17T14:51:57.127 回答