//初始化
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeCustom];
// button类型有以下6种,
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 十字加号按钮
//button设置frame
myButton.frame = CGRectMake(20, 20, 280,40);
//button背景色
myButton.backgroundColor = [UIColor clearColor];
//button背景图片
//[myButton setImage:[UIImage imageNamed:@"back.png" ] forState:UIControlStateNormal];
//设置button标题
[myButton setTitle:@"返回" forState:UIControlStateNormal];
// forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现
//以下是几种状态
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 <<0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留
//当按钮高亮的情况下,图像的颜色较深,将下面的这个属性设置为no,高亮取消
myButton.adjustsImageWhenHighlighted = NO;
//当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置
myButton.adjustsImageWhenDisabled = NO;
//设置为yes,按钮按下会发光,设置为no,无效果
myButton.showsTouchWhenHighlighted = YES;
// 给button添加事件
[myButton addTarget:self action:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
//显示
[self.view addSubview:myButton];
//button方法实现
-(void) btnClick:(UIButton*)btn{
NSLog(@”do something”);
}