//UIButton
// //button提供了通过便利构造器创建的方法
// UIButton *button = [UIButton buttonWithType: UIButtonTypeSystem];
// //设置尺寸
// button.frame=CGRectMake(100, 100, 150, 50);
// button.backgroundColor = [UIColor grayColor];
// [self.window addSubview:button];
// //通过这种方式创建,不需要写release
//
// //添加弧度和边框
// button.layer.cornerRadius = 10;
// button.layer.borderWidth=1;
//
// //给button设置标题
// [button setTitle:@"确定" forState:UIControlStateNormal];
// //标题颜色,如果是system,默认是蓝色
// [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//
// //设置标题字体大小
// button.titleLabel.font = [UIFont systemFontOfSize:25];
// //button的点击方法
// [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
-(void)click:(UIButton *)button{
//这个参数button就是触发方法对应的那个对象
// NSLog(@"测试一下");
// NSLog(@"%@",button.currentTitle);//当前标题
//点击button更换标题
if ([button.currentTitle isEqualToString:@"确定"]) {
[button setTitle:@"登陆" forState:UIControlStateNormal];
}
}
UIButton *but1 = [UIButton buttonWithType:UIButtonTypeSystem];
but1.frame = CGRectMake(100, 100, 150, 50);
[self.window addSubview:but1];
but1.layer.borderWidth = 1;
but1.layer.cornerRadius = 10;
//设置背景图片
// [but1 setBackgroundImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
[but1 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
//点击换图片
UIButton *but2 = [UIButton buttonWithType:UIButtonTypeCustom];
but2.frame = CGRectMake(100, 300, 150, 50);
but2.layer.borderWidth = 1;
but2.layer.cornerRadius = 10;
[self.window addSubview:but2];
[but2 setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
[but2 addTarget:self action:@selector(changeImage:) forControlEvents:UIControlEventTouchUpInside];
// [but2 setTitle:@"你好" forState:UIControlStateNormal];
// [but2 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
//custom是自定义的button,跟system没什么太大区别,但是在设置图片上有区别,一个是setBackGroundImage和setImage有区别,一个就是custom设置标题还需要额外设置标题颜色
-(void)changeImage:(UIButton *)button{
NSLog(@"%d",button.selected);
if (button.selected == NO) {
[button setImage:[UIImage imageNamed:@"BtnOn.png"] forState:UIControlStateNormal];
}
else {
[button setImage:[UIImage imageNamed:@"BtnOff.png"] forState:UIControlStateNormal];
}
//对属性进行取反,修改状态
button.selected = !button.selected;
}
UIButton
最新推荐文章于 2024-08-15 18:33:11 发布