//创建自定义按钮
UIButton*btn_click = [UIButtonbuttonWithType:UIButtonTypeCustom];
//创建普通状态按钮图片
[btn_clicksetImage:[UIImageimageNamed:@"home"]forState:UIControlStateNormal];
//创建高亮或选中状态按钮图片
[btn_clicksetImage[UIImageimageNamed:@"homeselected"]forState:UIControlStateHighlighted]
//设置按钮普通状态标题
[btn_clicksetTitle:@"我的主帖"forState:UIControlStateNormal];
//设置按钮字体
btn_click.titleLabel.font= [UIFontsystemFontOfSize:14];
//设置按钮普通状态标题颜色
[btn_clicksetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
//设置按钮高亮或选中状态标题颜色
[btn_clicksetTitleColor:[UIColoryellowColor]forState:UIControlStateHighlighted];
//设置按钮背颜色
[btn_clicksetBackgroundColor:[UIColorgrayColor]];
//按钮按下后的模糊状态
[btn_clicksetAdjustsImageWhenHighlighted:NO];
//按钮点击事件
[btn_clickaddTarget:selfaction:@selector(clickAction:)forControlEvents:UIControlEventTouchUpInside];
//按钮坐标和尺寸
btn_click.frame=CGRectMake(0,200,APP_WIGHT/2,49);
//按钮图片和标题总高度
CGFloattotalHeight =(btn_click.imageView.frame.size.height+btn_click.titleLabel.frame.size.height);
//设置按钮图片偏移
[btn_clicksetImageEdgeInsets:UIEdgeInsetsMake((totalHeightbtn_click.imageView.frame.size.height),0.0,0.0, -btn_click.titleLabel.frame.size.width)];
//设置按钮标题偏移
[btn_clicksetTitleEdgeInsets:UIEdgeInsetsMake(0.0, btn_click.imageView.frame.size.width, -(totalHeight - btn_click.titleLabel.frame.size.height),0.0)];
//加载按钮到视图
[self.viewaddSubview:btn_click];
关于UIButton,既可以采用setBackgroundImage 来设置底部图片,同时也可以采用 setImage方法;两者还是有一定区别的;
首先setBackgroundImage,image会随着button的大小而改变,图片自动会拉伸来适应button的大小,这个时候仍然可以设置button的title,title默认在button中间,image不会挡住title;
相反的的setImage,图片不会进行拉伸,原比例的显示在button上,此时再设置title,title默认显示在图右边,因此可以根据需求选中方法;