- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
/*----------------------UIButton的使用------------------------*/
// //创建按钮
//如果用这种方法,无法设置类型
// UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
// //设置按钮的类型
// button.buttonType = UIButtonTypeRoundedRect;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//设置显示的地方和大小
button.frame = CGRectMake(100, 50, 100, 50);
//设置背景颜色
// button.backgroundColor = [UIColor redColor];
//设置标题,设置标题一定要绑定状态
//设置普通状态显示的标题
[button setTitle:@"按钮" forState:UIControlStateNormal];
//设置点击(高亮)状态显示的标题
[button setTitle:@"点我了" forState:UIControlStateHighlighted];
//设置标题的颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
//按钮标题字体
button.titleLabel.font = [UIFont systemFontOfSize:15];
//文本位置
button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//偏移量
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//设置按钮的圆角属性
button.layer.cornerRadius = 5;
//添加按钮的点击事件
//UIControlEventTouchDown:按钮点击下的那一个事件就被触发了
//UIControlEventTouchDownRepeat按钮被多次点下的时候执行事件
//UIControlEventTouchDragInside 按钮被按着拖拽的时候被执行
//UIControlEventTouchUpInside 按钮被点击后松开 执行事件,常用的
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//设置button的tag值
button.tag = 1;
//添加到视图上显示
[_window addSubview:button];
//创建一个系统样式的按钮
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeContactAdd];
button1.frame = CGRectMake(100, 100, 50, 50);
button1.tag = 2;
//添加一个点击事件
[button1 addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button1];
/*-----------------------自定义按钮的使用-----------------------*/
//创建一个按钮
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
//设置frame
customButton.frame = CGRectMake(220, 200, 80, 40);
//创建两个图片
UIImage *image1 = [UIImage imageNamed:@"back_on_black.png"];
UIImage *image2 = [UIImage imageNamed:@"back_on.png"];
//设置button显示的图片,则按钮的标题不会出现了
//http://blog.csdn.net/u012762130/article/details/42591983
// [customButton setImage:image1 forState:UIControlStateNormal];
// [customButton setImage:image2 forState:UIControlStateHighlighted];
//禁用按钮,此时按钮的图片颜色会变浅
// button.enabled = NO;
//如果想显示图片还显示标题,则用一下方法
[customButton setBackgroundImage:image1 forState:UIControlStateNormal];
[customButton setBackgroundImage:image2 forState:UIControlStateHighlighted];
//设置标题
[customButton setTitle:@"按钮" forState:UIControlStateNormal];
[customButton setTitle:@"摸我了" forState:UIControlStateHighlighted];
button.userInteractionEnabled = NO;//交互事件
//设置点击事件
[customButton addTarget:self action:@selector(customAction) forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:customButton];
return YES;
}
- (void)customAction {
NSLog(@"点击了");
}
//按钮的点击事件
- (void)buttonAction:(UIButton *)button {
if (button.tag == 1) {
NSLog(@"button被点了");
}else if (button.tag == 2) {
NSLog(@"button1被点了");
}
}