一、xib方法,比较直观,推荐
自定义UIButton子类,如DKButton40,
DKButton40.h
#import <UIKit/UIKit.h>
@interface DKButton40 : UIButton
+(DKButton40 *)instanceButton;
@end
DKButton40.m
#import "DKButton40.h"
@implementation DKButton40
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
+(DKButton40 *)instanceButton{
NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"DKButton40" owner:nil options:nil];
return [nibView objectAtIndex:0];
}
DKButton40.xib
在Edge选项中可切换Title、Image在Button中的位置
使用方法:
chapterButton = [DKButton40 instanceButton];
然后可自定义按钮的图片及文字
二、直接使用代码调整
UIButton *back_btn = _headerToolView.backButton;
[back_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[back_btn setTitleColor:DK_BTN_TITLE_COLOR_HIGHLIGHT forState:UIControlStateHighlighted];
[back_btn setBackgroundColor:[UIColor clearColor]];
[back_btn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
[back_btn setTitle:@"返回" forState:UIControlStateNormal];
[back_btn.titleLabel setFont:[UIFont systemFontOfSize:DK_FONT_BIG_SIZE_BUTTON]];
<span style="white-space:pre"> </span><span style="color:#ff0000;">//这两句是关键</span>
[back_btn setImageEdgeInsets:UIEdgeInsetsMake(16, 5, 23, 10)];
[back_btn setTitleEdgeInsets:UIEdgeInsetsMake(12, 15, 0, 12)];
如果有多个按钮,要保证图片有相同尺寸,如此便可以确保UI的整齐