问题
I try to make an UIButton
with image upper the text, so I use this code:
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);
the problem is that with this code I don't see the text, that seems to be covered by the image, where is the mistake?
here the image:

回答1:
Try this
btn.imageEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
btn.titleEdgeInsets = UIEdgeInsetsMake(120, -120, 0, 0);
Adjust the value of -120
i.e. left to horizontally fit your title
回答2:
Can you please modify your code as follows?
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/uiedgeinset-issue-positioning-text-image