我尝试在文本上方创建一个带有图像的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);问题是,在这段代码中,我看不到文本,它似乎被图像所覆盖,错误在哪里?
下面是图片:

发布于 2012-12-17 22:50:57
尝尝这个
btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, -120, 0, 0);调整-120的值,即left以水平适合您的标题
发布于 2012-12-17 22:51:57
你能按如下方式修改你的代码吗?
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];https://stackoverflow.com/questions/13915898
复制相似问题