UIButton是按钮视图控件,是UIView的子类,也拥有和view一样的属性,同时也有自己特有的属性;
UIButton的主要作用是用来响应,或控制其他事件的发生;比如说显示,或隐藏其他控件,或调用其他函数方法。
- UILabel *lable01 = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 40.0)];
- lable01.backgroundColor = [UIColor yellowColor];
- lable01.text = @"按钮在控制我的显示,或隐藏";
- lable01.textColor = [UIColor redColor];
- [self.view addSubview:lable01];
- lable01.hidden = YES;
- lable01.center = self.view.center;
- lable01.tag = 2000;
- // 实例化
- UIButton *button001 = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 50.0, 120.0, 40.0)];
- // 是view的子类,拥有view一样的属性
- [self.view addSubview:button001];
- // button001.backgroundColor = [UIColor yellowColor];
- // button001.layer.cornerRadius = 5.0;
- // button001.layer.masksToBounds = YES;
- // button001.layer.borderWidth = 0.5;
- // button001.layer.borderColor = [UIColor redColor].CGColor;
- button001.tag = 1000;
- // 自己特有的属性
- // 1 选中状态,默认是NO,即未选中;通常结合响应方法进行使用
- button001.selected = NO;
- // 2 按钮标题颜色,可以根据响应状态进行设置,比如常规,或高亮,或选中等
- [button001 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- [button001 setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
- [button001 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
- // 3 按钮标题
- // 3-1 大小
- button001.titleLabel.font = [UIFont systemFontOfSize:12.0];
- // 3-2 对齐方式,默认居中
- // button001.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
- // button001.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
- // 4 按钮标题,同样也可以根据响应状态进行设置
- [button001 setTitle:@"显示标签" forState:UIControlStateNormal];
- [button001 setTitle:@"隐藏标签" forState:UIControlStateHighlighted];
- [button001 setTitle:@"隐藏标签" forState:UIControlStateSelected];
- // 5 设置图片,默认图标在左,标题在右,还可通过设置改变显示样式,比如图片在上,标题在下,或图片在右,标题在左。
- [button001 setImage:[UIImage imageNamed:@"imageNormal"] forState:UIControlStateNormal];
- [button001 setImage:[UIImage imageNamed:@"imageSelected"] forState:UIControlStateSelected];
- // 6 设置背景图片
- [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageNormal"] forState:UIControlStateNormal];
- [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageHighlight"] forState:UIControlStateHighlighted];
- [button001 setBackgroundImage:[UIImage imageNamed:@"bgImageSelected"] forState:UIControlStateSelected];
- // 7 其他属性
- // 7-1 如当前显示标题
- NSString *title = button001.currentTitle;
- NSLog(@"title=%@",title);
- // 7-2 点击属性,默认为YES,即可点击
- // 方法1
- // BOOL isEnable = button001.enabled;
- // NSLog(@"isEnable=%@",@(isEnable));
- // button001.enabled = NO; // 设置为不可用
- // isEnable = button001.enabled;
- // NSLog(@"isEnable=%@",@(isEnable));
- // 方法2 view的userInteractionEnabled属性,默认为YES,即可点击
- BOOL isEnable = button001.userInteractionEnabled;
- NSLog(@"isEnable=%@",@(isEnable));
- button001.userInteractionEnabled = NO; // 设置为不可用
- isEnable = button001.userInteractionEnabled;
- NSLog(@"isEnable=%@",@(isEnable));
- // target方法
- [button001 addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
- - (void)buttonClick:(UIButton *)button
- {
- button.selected = !button.selected;
- UILabel *label = (UILabel *)[self.view viewWithTag:2000];
- label.hidden = !button.selected;
- NSLog(@"按钮被点击了");
- }