一、按鈕上是文字
1.初始化, 由于UIButton 有自己的初始化方式,所以用自己的
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
(后面的按鈕類型是這個按鈕的樣子,有如下按鈕類型)
// UIButtonTypeDetailDisclosure, //文字前面有圓圈,圓圈里面有i的按鈕
// UIButtonTypeInfoLight, //文字前面有圓圈,圓圈里面有i的按鈕
// UIButtonTypeInfoDark, //文字前面有圓圈,圓圈里面有i的按鈕
// UIButtonTypeContactAdd, //文字前面有圓圈,圓圈里面有+的按鈕
2.為按鈕設(shè)置標題
[button setTitle:@"點我" forState:UIControlStateNormal];
(后面的是按鈕的上標題的一些規(guī)定)
// UIControlStateNormal 常態(tài) //一般模式的標題規(guī)定
// UIControlStateHighlighted 高亮 //當點擊按鈕時,并且不放,此標題才會顯示出來
// UIControlStateDisabled //已經(jīng)被禁止使用
// UIControlStateSelected 被選中 //是個BOOL類型,默認是NO, 是按鈕的屬性eg:
button.selected=!button.selected; 和此按鈕的方法配合
使用,類似手機上的收藏鍵(文字四周還有框)
3.設(shè)置按鈕背景顏色 就是程序運行后按鈕的背景色
[button setBackgroundColor: [UIColor redColor]];
4.修改按鈕上 標題的字體顏色 (默認黑色)
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
后面用selected指的是按鈕被點擊后 被選擇標題的顏色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
下面的方法改變了工程運行后出來的按鈕上的字體顏色和點擊按鈕后被選中的字體的背景(不是整個
按鈕的背景),如果單獨設(shè)置工程運行后出來的按鈕上的字體的顏色,則此字體用該顏色
[button setTintColor: [UIColor redColor] ];
5.修改按鈕上字體的大小
button.titleLable.font=[UIFont systemFontOfSize:20 ];
6.為按鈕添加點擊事件
[button addTarget: self anction: @selector(btnAction:) forControlEvents: UIControlEventTouchUpInside];
//Target:動作的執(zhí)行目標,方法的執(zhí)行體(就是按鈕的action事件寫在那個類里面)
//target-action 目標動作機制
//action:按鈕的回調(diào)方法,如果方法有參數(shù),那么這個參數(shù)一定是按鈕本身,方法沒有參數(shù)也可以(沒有參數(shù)時可以不用寫冒號)
//events:那種觸摸方式會觸發(fā)按鈕的回調(diào)方法
下面是幾種觸摸方式
// UIControlEventTouchDragInside 點擊不松手輕輕移動觸發(fā)
// UIControlEventTouchDragOutside 點擊不松手,向其他位置拖觸發(fā)
// UIControlEventTouchDragEnter
// UIControlEventTouchDragExit
// UIControlEventTouchUpInside 單擊:手指按下去,再松手的時候觸發(fā)的(最常用點擊方式)
// UIControlEventTouchUpOutside
// UIControlEventTouchCancel
// UIControlEventTouchDown 按鈕點下去之后就會出觸發(fā)方法,不等松手
// UIControlEventTouchDownRepeat 連擊兩下觸發(fā)
7.按鈕被點擊后的觸發(fā)事件 選中與被選中之間的切換
//按鈕的回調(diào)方法,此方法就是觸發(fā)該方法的按鈕
-(void)btnAction:(UIButton *)sender
{
sender.selected=!sender.selected;
}
二、按鈕上是圖片
1.初始化 Custom是自定義的意思
UIButton *button=[UIButton buttonWithType: UIButtonTypeCustom];
2.為按鈕添加圖片
[button setImage:[UIImage imageNamed: @"shipping.jpg"]
forState:UIControlStateNormal ];
3.設(shè)置回調(diào)方法
[button addTarget: self action:@selector(customAction:) forControlEvents:
UIControlEventTouchUpInside ];
4.將按鈕添加到父視圖上
[self.window addSubview:button];
5.給高亮狀態(tài)下托進一張圖片
[button setImage:[UIImage imageNamed:@"勇者.png"] forState:UIControlStateHighlighted];
6.定義一個得到按鈕的圖片并顯示在UIImageView上 如果圖片是常態(tài)下地就裝換成高亮成高亮狀態(tài)
下,如果是高亮狀態(tài)下地圖片,就轉(zhuǎn)換為常態(tài)下的圖片
UIImageView *btnImageView=[[UIImageView alloc]initWithFrame:CGRectMake(100,100, 100,
100)];
btnImageView.tag=1000;
btnImageView.center=self.window.center;
[self.window addSubview:btnImageView];
7.獲取常態(tài)button上的圖片
[button imageForState:UIControlStateNormal];
8.獲取常態(tài)button上的文字
[button titleForState:UIControlStateNormal];
9.button被點擊
button.selected==YES;
10.返回上一界面
-(void)cancelAction:(UIButton *)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
11.方法實現(xiàn)
-(void)customBtnAction:(UIButton *)sender
{
NSLog(@"這是自定義按鈕的回調(diào)方法");
//得到按鈕的圖片并顯示在UIImageView上 如果圖片是常態(tài)下地就裝換成高亮成高亮狀態(tài)下,如果是高亮狀態(tài)下地圖片,就轉(zhuǎn)換為常態(tài)下的圖片
UIImageView *imageView=(UIImageView *)[self.window viewWithTag:1000];
imageView.center=CGPointMake(self.window.center.x, 100);
//button獲取它上面的圖片
UIImage *btnImage=[sender imageForState:UIControlStateNormal];
imageView.image=btnImage;
//判斷:按鈕的狀態(tài)是常態(tài),還是高亮
if(sender.selected==YES)
{
sender.selected=NO;
UIImage *btnImage=[sender imageForState:UIControlStateNormal];
imageView.image=btnImage;
}
else
{
sender.selected=YES;
UIImage *btnImage=[sender imageForState:UIControlStateHighlighted];
imageView.image=btnImage;
}
}