1、创建按钮
UIButton* btn=[ [ UIButton alloc ] init ];
2、添加按钮
[ self.view addSubview : btn ];
3、设置位置和大小
btn.frame = CGRectMake( 100, 100, 100, 100 );
4、设置背景颜色或者背景图片
//[btn setBackgroundColor:[UIColor yellowColor]];//设置黄色背景
UIImage *tu1 = [ UIImage imageNamed : @"image1" ];
[ btn setBackgroundImage:tu1 forState:UIControlStateNormal];
UIImage *tu2 = [ UIImage imageNamed:@"image2" ];
[btn setBackgroundImage:tu2 forState:UIControlStateHighlighted];
5、设置文字
[ btn setTitle:@"普通状态" forState:UIControlStateNormal ];
[ btn setTitle:@"高亮状态" forState:UIControlStateHighlighted ];
[ btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal ];
[ btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted ];
6、监听按钮消息
[ btn addTarget:self action:@selector(btnMsg:) forControlEvents:UIControlEventTouchUpInside ];
- (void)btnMsg:(UIButton *)button
{
NSLog(@"*******%@", button);
}