不说废话,直接上代码,温故而知新,确实受益良多!其中的奥妙,自己体会!
#import "ViewController.h"
#define screen_width [UIScreen mainScreen].bounds.size.width
#define screen_height [UIScreen mainScreen].bounds.size.height
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self createButton];
}
- (void)createButton
{
float bw = 50;
float bh = 50;
//设置button的类型
/*
UIButtonTypeCustom 自定
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0) iOS7默认
UIButtonTypeDetailDisclosure 感叹号
UIButtonTypeInfoLight 感叹号
UIButtonTypeInfoDark 感叹号
UIButtonTypeContactAdd 加号
*/
UIButton * b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置frame值
b.frame = CGRectMake((screen_width - bw) / 2, (screen_height - bh) / 2, bw, bh);
//设置背景色
b.backgroundColor = [UIColor redColor];
//设置按钮标题
[b setTitle:@"按钮" forState:(UIControlStateNormal)];
//设置按钮标题颜色
[b setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
//设置不同状态下文本阴影色
[b setTitleShadowColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
//添加背景图片
[b setImage:[UIImage imageNamed:@"1.jpg"] forState:(UIControlStateNormal)];
//为button添加点击事件
[b addTarget:self action:@selector(buttonClicked:) forControlEvents:(UIControlEventTouchUpInside)];
//为button添加tag值
b.tag = 200;
[self.view addSubview:b];
}
- (void)buttonClicked:(UIButton *)sender
{
//设置背景图片
[sender setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:(UIControlStateNormal)];
[sender setTitle:@"选中" forState:(UIControlStateSelected)];
sender.selected = YES;
}