我尝试在图片上方创建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);
问题是,使用此代码我看不到文本,这似乎是图像所涵盖的,错误在哪里?
这里的图像:
答案 0 :(得分:3)
试试这个
btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, -120, 0, 0);
调整-120
的值,即左以横向调整您的标题
答案 1 :(得分: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];