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#>)];