btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最后将按钮加入到指定视图superView
[superView addSubview: btn];
附:创建按钮的两种方法:
1、动态创建
btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnfont setFrame:CGRectMake(100, 10, 120, 40)];
[btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[btnfont setTitle:@"字体" forState:UIControlStateNormal];
btnfont.backgroundColor=[UIColor clearColor];
[self.view addSubview:btnfont];
2、在xib文件中已经创建好,通过tag获取按钮
UIButton *testButton= (UIButton*)[self.view viewWithTag:100];
[testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
注册事件
-(void) test: (id) sender{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"ceshi" message:@"test11111" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];
[av show];
}
UIButton状态:
UIControlStateNormal // 正常状态
UIControlStateHighlighted // 高亮状态
UIControlStateDisabled // 禁用状态
UIControlStateSelected // 选中状态
UIControlStateApplication //
UIControlStateReserved // 保留状态
UIButton类型:
UIButtonTypeCustom //自定义类型
添加图片:

灰色背景颜色:

UIButtonTypeRoundedRect //圆角类型

UIButtonTypeDetailDisclosure //细节展示按钮

UIButtonTypeInfoLight //浅色背景的信息按钮

UIButtonTypeInfoDark //暗色背景的信息按钮

UIButtonTypeContactAdd // 添加按钮 www.2cto.com

创建UIButton
1. UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake (x, y, Width, Height)];
2. UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; 设置UIButton标题
[button setTitle:title forState:UIControlStateNormal]; 设置UIButton标题颜色
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 设置UIButton背景图片
[button setBackgroundImage:newImage forState:UIControlStateNormal]; 设置UIButton背景颜色
button.backgroundColor = [UIColor clearColor];