補(bǔ)充
- 按鈕自動(dòng)適應(yīng)按鈕的內(nèi)容的大小
[button sizeToFit ]
- 設(shè)置按鈕內(nèi)容排序的方向
(UIControlContentHorizontalAlignmentLeft;UIControlContentVerticalAlignment)內(nèi)容向左邊排序
[button setContentHorizontalAlignment: UIControlContentHorizontalAlignmentLeft];
- 設(shè)置按鈕的內(nèi)容偏移量
button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
- 讓按鈕不能點(diǎn)擊(默認(rèn)是可以點(diǎn)擊的)
button3.enabled = NO;
[button3 setTitle:@"點(diǎn)不了" forState:UIControlStateDisabled];
- 對(duì)子控件圖片,文字布局做排版
[self.clearRecentHistoryButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 15, 0, 0)];
[self.clearRecentHistoryButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
- 關(guān)閉高亮狀態(tài)
button.adjustsImageWhenHighlighted = NO;
在XIB中的話,取消勾選的 "highlight adjusts image"
目錄
- 給按鈕添加事件(核心方法)
- 系統(tǒng)按鈕
- 文字按鈕
- 圖片按鈕
- 圖片和文字同時(shí)存在
- 定制按鈕(圖片和文字的位置)
- 獲取按鈕不同狀態(tài)下的文字或者圖片
給按鈕添加事件(核心方法)
[button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchDown];
當(dāng)指定的事件發(fā)生的時(shí)候,響應(yīng)消息的對(duì)象會(huì)去調(diào)用指定的消息
- 參數(shù)1:響應(yīng)消息的對(duì)象
- 參數(shù)2:消息 (可以不帶參,但是如果帶參只能有一個(gè)參數(shù),并且這個(gè)參數(shù)的實(shí)參就是按鈕本身) --必須實(shí)現(xiàn)
- 參數(shù)3:事件 UIControlEvent
UIControlEventTouchUpInside 按下松開(kāi)
UIControlEventTouchDown 按下
UIControlEventTouchUpInside:當(dāng)手指按下按鈕并且松開(kāi)的一瞬間,self去調(diào)用onclicked:方法
UIControlEventTouchDown:當(dāng)手指按下按鈕的瞬間,self去調(diào)用on clicked:方法
系統(tǒng)按鈕
創(chuàng)建按鈕
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
- UIButtonTypeCustom 自定制類(lèi)型相當(dāng)于[[UIButton alloc] init]創(chuàng)建的按鈕
- UIButtonTypeSystem 設(shè)置系統(tǒng)按鈕,相當(dāng)于[[UIButton alloc] init],不能去設(shè)置圖片和文字(屬于淘汰的枚舉值)
- 參數(shù):系統(tǒng)按鈕
UIButtonTypeDetailDisclosure, 系統(tǒng)詳情按鈕
UIButtonTypeInfoLight, 系統(tǒng)詳情按鈕
UIButtonTypeInfoDark, 系統(tǒng)詳情按鈕
UIButtonTypeContactAdd, 系統(tǒng)添加按鈕
UIButtonTypeRoundedRect = UIButtonTypeSystem
文字按鈕
[button3 setTitle:@"來(lái)點(diǎn)我啊" forState:UIControlStateNormal];
- 給不同狀態(tài)設(shè)置標(biāo)題(讓按鈕上顯示文字,只能通過(guò)這個(gè)方法來(lái)設(shè)置)
- 參數(shù)1:標(biāo)題(按鈕上顯示的文字)
- 參數(shù)2:
UIControlStateNormal 正常狀態(tài) ,如果只設(shè)置了正常狀態(tài)下的文字,那么其他的狀態(tài)下文字和正常狀態(tài)下文字一樣
UIControlStateHighlighted 高亮狀態(tài)
UIControlStateDisabled 不可點(diǎn)擊狀態(tài)
UIControlStateSelected 選中狀態(tài)
- 給不同狀態(tài)設(shè)置不一樣的文字顏色
[button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
- 文字字體
titleLabel就是按鈕上專(zhuān)門(mén)用來(lái)顯示文字的部分,但是不能通過(guò)拿到這個(gè)label來(lái)設(shè)置按鈕上的文字和文字顏色,只能設(shè)置字體
button3.titleLabel.font = [UIFont systemFontOfSize:25];
圖片按鈕
- 給不同狀態(tài)設(shè)置不同圖片
- 參數(shù)1:圖片
- 參數(shù)2:按鈕狀態(tài)
- 設(shè)置按鈕背景圖片
[button setBackgroundImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
- 設(shè)置正常狀態(tài)下的圖片
[button4 setImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
- 設(shè)置高亮狀態(tài)下的圖片
[button4 setImage:[UIImage imageNamed:@"button_up.png"] forState:UIControlStateHighlighted];
- 設(shè)置內(nèi)容到按鈕邊界的邊距:上、左、下、右
[button4 setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
圖片和文字同時(shí)存在
- 設(shè)置文字(文字顏色默認(rèn)是白色)
[button5 setTitle:@"hello" forState:UIControlStateNormal];
- 設(shè)置文字顏色
[button5 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
- 3.設(shè)置圖片
[button5 setImage:[UIImage imageNamed:@"player_down_1"] forState:UIControlStateNormal];
定制按鈕(圖片和文字的位置)
創(chuàng)建繼承UIButton的類(lèi)
@interface CustomButton : UIButton
//imageViewH:labelH = 4 : 1
//重新計(jì)算按鈕中l(wèi)abel的位置信息
//contentRect:當(dāng)前按鈕的frame(真正有用的是size)
//返回值:label新的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect{
CGFloat W = contentRect.size.width;
CGFloat X = 0;
CGFloat H = contentRect.size.height/5.0f;
CGFloat Y = contentRect.size.height/5.0f * 4;
return CGRectMake(X, Y, W, H);
}
//重新計(jì)算imageView的位置信息
//contentRect:當(dāng)前按鈕的frame(真正有用的是size)
//返回值:imageView新的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect{
CGFloat W = contentRect.size.width;
CGFloat X = 0;
CGFloat H = contentRect.size.height/5.0f * 4;
CGFloat Y = 0;
return CGRectMake(X, Y, W, H);
}
獲取按鈕不同狀態(tài)下的文字或者圖片
//獲取某個(gè)狀態(tài)下的文字或者圖片
button titleForState:<#(UIControlState)#>
button imageForState:<#(UIControlState)#>
//當(dāng)前狀態(tài)下的文字或者圖片
[button currentTitle];
[button currentImage];