2.狀態:UIControlStateNormal=0 常規狀態顯現
UIControlStateHighlighted=1<<0高亮狀態顯現
*/
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(170, 200, 70, 70);
//button.backgroundColor=[UIColor blackColor];
//設置常規標題
[button setTitle:@"登錄" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//設置button上的字體大小
button.titleLabel.font=[UIFont systemFontOfSize:18];
//設置高亮標題
// [button setTitle:@"高亮" forState:UIControlStateHighlighted];
// [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
//按鈕是否禁用
// button.enabled=NO;
// [button setTitle:@"禁用" forState:UIControlStateDisabled];
// [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
//判斷按鈕是否選中
// button.selected=YES;
[button setTitle:@"被選中" forState:UIControlStateSelected];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
//設置圓角
button.layer.cornerRadius=50;
button.layer.masksToBounds=YES;
//設置背景圖片
//UIImage *image=[UIImage imageNamed:@"Icon-80.png"];
//[button setBackgroundImage:image forState:UIControlStateNormal];
UIImage *image1=[UIImage imageNamed:@"649417.jpg"];
[button setBackgroundImage:image1 forState:UIControlStateSelected];
// [button setImage:image forState:UIControlStateNormal];
//[button setImage:image1 forState:UIControlStateHighlighted];
//一點就會高亮
// button.showsTouchWhenHighlighted=YES;
//@selector(selector)方法選擇器
//監聽
[button addTarget:self action:@selector(text:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(text1:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
view.backgroundColor=[UIColor yellowColor];
view.tag=110;
[self.view addSubview:view];
}
-(void)text1:(UIButton *)button{
if (button.selected==NO) {
button.selected=YES;
}else{
button.selected=NO;
}
}
-(void)text:(UIButton *)sender{
UIView *view=[self.view viewWithTag:110];
view.backgroundColor=[UIColor colorWithRed:(arc4random()%(256-0+1)+0)/256.0 green:(arc4random()%(256-0+1)+0)/256.0 blue:(arc4random()%(256-0+1)+0)/256.0 alpha:1];
view.frame=CGRectMake(arc4random()%(414-50-0+1)+0, arc4random()%(736-0+1)+0-50, arc4random()%(414-0+1)+0-50,50);
UIButton屬性
1.UIButton狀態:
UIControlStateNormal // 正常狀態
UIControlStateHighlighted // 高亮狀態
UIControlStateDisabled // 禁用狀態
UIControlStateSelected // 選中狀態
UIControlStateApplication //
UIControlStateReserved // 保留狀態
2.Uibutton類型:
UIButtonTypeCustom //自定義類型
添加圖片: 灰色背景顏色:
UIButtonTypeRoundedRect //圓角類型
UIButtonTypeDetailDisclosure //細節展示按鈕
UIButtonTypeInfoLight //淺色背景的信息按鈕
UIButtonTypeInfoDark //暗色背景的信息按鈕
UIButtonTypeContactAdd // 添加按鈕
3.UIButton常用屬性
給按鈕設置文字時,蘋果文檔說明,不能使用label對象設置文字的顏色或者陰影顏色,相反必須使用setTitleColor:forState: and setTitleShadowColor:forState:這兩個方法才能修改這些值。
設置按鈕中其他屬性依次類推。。。。
//設置對應狀態的標題內容default is nil. title is assumed to be single line
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//設置對應狀態的標題顏色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//設置對應狀態的標題陰影顏色
- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
//設置對應狀態的按鈕的圖片
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//設置對應狀態的按鈕背景圖片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
添加事件
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
這些事件都是基於觸摸、基於值、基於編輯。有如下事件會觸發。
在點擊按鈕是按鈕是凹下去,然后彈起才觸發起事件,按鈕的狀態有:
- UIControlEventTouchDown // 按下
- UIControlEventTouchDownRepeat 多次按下
- UIControlEventTouchUpInside // 在按鈕及其一定外圍內松開
- UIControlEventTouchUpOutside // 按鈕外面松開
4.adjustsImageWhenDisabled
當按鈕禁用的情況下,圖像的顏色會被畫深一點,默認為YES。
5.adjustsImageWhenHighlighted
當按鈕高亮的情況下,圖像的顏色會被畫深一點,默認為YES。
6.showsTouchWhenHighlighted
button.showsTouchWhenHighlighted=YES;點擊時的閃光效果會被前景圖片遮住中間部分;
6.contentEdgeInsets
設置按鈕的內部內容(包含按鈕圖片和標題)離按鈕邊緣上下左右的距離。
7.按鈕實例
1.有些時候我們想讓UIButton的title居左對齊,我們設置
btn.textLabel.textAlignment = UITextAlignmentLeft
是沒有作用的,我們需要設置
btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;
但是問題又出來,此時文字會緊貼到左邊框,我們可以設置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距離左邊框保持10個像素的距離。