UIButton
Button是一個(gè)繼承于UIView的矩形按鈕對象
UIButton初始化
//創(chuàng)建了一個(gè)btn對象,根據(jù)類型類創(chuàng)建btn,
//圓角類型btn:UIButtonTypeRoundedRect
//通過類方法來創(chuàng)建buttonWithType:類名+方法名
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
設(shè)置button 按鈕的位置
btn.frame= CGRectMake(100, 100, 80, 40);
設(shè)置按鈕的文字內(nèi)容
//P1:字符串類型,顯示到按鈕上的文字
//P2:設(shè)置文字類型的狀態(tài)類型:
UIControlStateNormal,正常狀態(tài)
UIControlStateHighlighted,按下狀態(tài)
[btn setTitle:@"按鈕01" forState:UIControlStateNormal];
[btn setTitle:@"按鈕按下" forState:UIControlStateHighlighted];
button 背景色設(shè)置
btn.backgroundColor = [UIColor grayColor];
button文字的相關(guān)設(shè)置
btn.titleLabel.font = [UIFont systemFontOfSize:24];
按鈕風(fēng)格顏色設(shè)置
//設(shè)置文字顯示的顏色
//P1:顏色
//P2:狀態(tài)
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
//設(shè)置按下狀態(tài)的顏色
[btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
//設(shè)置按鈕的風(fēng)格顏色
[btn setTintColor:[UIColor whiteColor]];
將button顯示出來
//添加到視圖中并顯示
[self.view addSubview:btn];
顯示圖片的button
//三步創(chuàng)建一個(gè)自定義類型的btn
//custom 定制的,定做的
UIButton * btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
//創(chuàng)建UIImage對象
UIImage * icon01 = [UIImage imageNamed:@"btn01.jpg"];
UIImage * icon02 = [UIImage imageNamed:@"btn02.jpg"];
//設(shè)置按鈕圖片方法設(shè)置
//p1:顯示的圖片對象
//p2:控件的狀態(tài)
[btnImage setImage:icon01 forState:UIControlStateNormal];
[btnImage setImage:icon02 forState:UIControlStateHighlighted];
[self.view addSubview:btnImage];
button事件處理
//向按鈕添加事件函數(shù)
//P1:“誰”來實(shí)現(xiàn)事件函數(shù),實(shí)現(xiàn)的對象就是"誰"
//P2:@selector(pressBtn):函數(shù)對象,當(dāng)按鈕滿足P3事件類型時(shí),調(diào)用函數(shù)
//P3:forControlEvents:事件處理函數(shù)
//UIControlEventTouchUpInside:當(dāng)手指離開屏幕時(shí)并且手指的位置在按鈕范圍內(nèi)觸發(fā)事件函數(shù)
//UIControlEventTouchDown:當(dāng)我們的手指觸碰到屏幕上時(shí)
[btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
//觸碰時(shí)調(diào)用事件函數(shù)
[btn addTarget:self action:@selector(touchDown) forControlEvents:UIControlEventTouchDown];
控件狀態(tài)
普通(Normal):最常見的狀態(tài)是默認(rèn)的普通狀態(tài)??丶谖刺幱谄渌麪顟B(tài)時(shí)都說這種狀態(tài)
突出顯示(Highlighted):突出顯示狀態(tài)是控件正被使用時(shí)的狀態(tài)。對于按鈕來說,這表示用戶手指正在按鈕上
禁用(Disabled):禁用狀態(tài)是控件被關(guān)閉時(shí)的狀態(tài)。用禁用控件,可以在Interface Builder中取消選中Enabled 復(fù)選框,或者將控件的enabled屬性設(shè)置為NO。
選中(Selected):只有一部分控件支持選中狀態(tài)。它通常用于指示該控件已啟動或被選中。選中狀態(tài)與突出顯示狀態(tài)類似,但控件可以在用戶不再直接使用它時(shí)繼續(xù)保持選中狀態(tài)。