UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];
[button setTitle:@"HHH" forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"narrowend.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"panelicon.png"] forState:UIControlStateNormal];
默认情况下,图片及按钮文字如下:

加入如下代码后
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, -20, 0.0, 0.0)];

[button setImageEdgeInsets:UIEdgeInsetsMake(<#CGFloat top#向上偏移量>, <#CGFloat left#向左偏移量>, <#CGFloat bottom#向下偏移量>, <#CGFloat right#向右上偏移量>)];
同时还可以设置文字的偏移量:
[button setTitleEdgeInsets:UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right>
网友评论