UIButton是按鈕視圖控件,是UIView的子類,也擁有和view一樣的屬性,同時也有自己特有的屬性;
UIButton的主要作用是用來響應,或控制其他事件的發生;比如說顯示,或隱藏其他控件,或調用其他函數方法。
- UILabel*lable01=[[UILabelalloc]initWithFrame:CGRectMake(0.0,0.0,300.0,40.0)];
- lable01.backgroundColor=[UIColoryellowColor];
- lable01.text=@"按鈕在控制我的顯示,或隱藏";
- lable01.textColor=[UIColorredColor];
- [self.viewaddSubview:lable01];
- lable01.hidden=YES;
- lable01.center=self.view.center;
- lable01.tag=2000;
- //實例化
- UIButton*button001=[[UIButtonalloc]initWithFrame:CGRectMake(10.0,50.0,120.0,40.0)];
- //是view的子類,擁有view一樣的屬性
- [self.viewaddSubview:button001];
- //button001.backgroundColor=[UIColoryellowColor];
- //button001.layer.cornerRadius=5.0;
- //button001.layer.masksToBounds=YES;
- //button001.layer.borderWidth=0.5;
- //button001.layer.borderColor=[UIColorredColor].CGColor;
- button001.tag=1000;
- //自己特有的屬性
-
- //1選中狀態,默認是NO,即未選中;通常結合響應方法進行使用
- button001.selected=NO;
- //2按鈕標題顏色,可以根據響應狀態進行設置,比如常規,或高亮,或選中等
- [button001setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
- [button001setTitleColor:[UIColorblackColor]forState:UIControlStateHighlighted];
- [button001setTitleColor:[UIColorgreenColor]forState:UIControlStateSelected];
- //3按鈕標題
- //3-1大小
- button001.titleLabel.font=[UIFontsystemFontOfSize:12.0];
- //3-2對齊方式,默認居中
- //button001.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
- //button001.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
- //4按鈕標題,同樣也可以根據響應狀態進行設置
- [button001setTitle:@"顯示標簽"forState:UIControlStateNormal];
- [button001setTitle:@"隱藏標簽"forState:UIControlStateHighlighted];
- [button001setTitle:@"隱藏標簽"forState:UIControlStateSelected];
- //5設置圖片,默認圖標在左,標題在右,還可通過設置改變顯示樣式,比如圖片在上,標題在下,或圖片在右,標題在左。
- [button001setImage:[UIImageimageNamed:@"imageNormal"]forState:UIControlStateNormal];
- [button001setImage:[UIImageimageNamed:@"imageSelected"]forState:UIControlStateSelected];
- //6設置背景圖片
- [button001setBackgroundImage:[UIImageimageNamed:@"bgImageNormal"]forState:UIControlStateNormal];
- [button001setBackgroundImage:[UIImageimageNamed:@"bgImageHighlight"]forState:UIControlStateHighlighted];
- [button001setBackgroundImage:[UIImageimageNamed:@"bgImageSelected"]forState:UIControlStateSelected];
- //7其他屬性
- //7-1如當前顯示標題
- NSString*title=button001.currentTitle;
- NSLog(@"title=%@",title);
-
- //7-2點擊屬性,默認為YES,即可點擊
- //方法1
- //BOOLisEnable=button001.enabled;
- //NSLog(@"isEnable=%@",@(isEnable));
- //button001.enabled=NO;//設置為不可用
- //isEnable=button001.enabled;
- //NSLog(@"isEnable=%@",@(isEnable));
- //方法2view的userInteractionEnabled屬性,默認為YES,即可點擊
- BOOLisEnable=button001.userInteractionEnabled;
- NSLog(@"isEnable=%@",@(isEnable));
- button001.userInteractionEnabled=NO;//設置為不可用
- isEnable=button001.userInteractionEnabled;
- NSLog(@"isEnable=%@",@(isEnable));
- //target方法
- [button001addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
- -(void)buttonClick:(UIButton*)button
- {
- button.selected=!button.selected;
-
- UILabel*label=(UILabel*)[self.viewviewWithTag:2000];
- label.hidden=!button.selected;
-
- NSLog(@"按鈕被點擊了");
- }
-