纯代码创建Button控件:

纯代码创建Button控件:

// 创建按钮
UIButton *btn = [[UIButton alloc] init];

// 添加按钮
[self.view addSubview:btn];

// 设置frame
btn.frame = CGRectMake(50, 50, 100, 100);

// 设置背景图片
// 通过文件名加载图片(凡是PNG图片,都不用加拓展名)
UIImage *normal = [UIImage imageName:@“btn_01”];
// 设置普通状态下的背景图片
[btn setBackgroundImage:normal forState:UIControlStateNormal];

// 加载高亮的图片
UIImage *hight = [UIImage imageName:@“btn_02”];
[btn setBackgroundImage:normal forState:UIControlStateHighlighted];

// 设置文字
[btn setTitle:@“按钮1” forState:UIControlStateNormal];
[btn setTitle:@“按钮2” forState:UIControlStateHighlighted];

// 设置文字颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHightlighted];
原文地址:https://www.cnblogs.com/pocket-mood/p/4331293.html