最為最基本的控件,我們必須對button的每個常用屬性都熟練應用;
1,使用之前,必須對按鈕進行定義,為樂規范,在@interface ViewController (){}中進行定義,先定義后使用。
UIButton *_button1; 在實際的項目開發中,變量名盡可能長一點,並且在變量名前加"_" 下划線字符(規范)
2,接下來在@implementation ViewController中對按鈕的屬性進行賦值
_button1=[UIButton buttonWithType:UIButtonTypeRoundedRect]; //設置按鈕的樣式
UIButtonTypeCustom = 0, 自定義風格
UIButtonTypeRoundedRect, 圓角矩形
UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用
UIButtonTypeInfoLight, 亮色感嘆號
UIButtonTypeInfoDark, 暗色感嘆號
UIButtonTypeContactAdd, 十字加號按鈕
3,為按鈕設置背景顏色
_button1.backgroundColor=[UIColor whiteColor];
4,設置按鈕在圖中的顯示位置和大小
_button1.frame=CGRectMake(100, 100, 40, 40);
5,給按鈕起名字和名字的顏色
[_button1 setTitle:@"點擊" forState:UIControlStateNormal]; (點擊是按鈕的名字)
[_button setTitleColor:[UIColorredColor]forState:UIControlStateNormal];
6,按鈕也有好多狀態
forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯
現在只會用UIControlStateNormal 其他的暫時用不到
7,按鈕上可以放圖片
[_button1 setImage:[UIImageimageNamed:@"11.png"]forState:UIControlStateNormal];
[_button1 setBackgroundImage:[UIImageimageNamed:@"22.png"]forState:UIControlStateNormal];
8,關於按鈕顯示時,總顯示是方形的問題
[startBtn.layer setCornerRadius:8];//設置按鈕圓弧的角度
加入這句可以使按鈕變成圓角
9,點擊按鈕,使按鈕有所反應(添加事件)
[_button1 addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; (start是函數名)
10,按鈕在普通狀態和高亮狀態顏色的設置
[_button1 setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted]; //在按鈕被按下去的,按鈕的整個會變成黃色
[_button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];//按鈕在普通狀態下,將按鈕上的標題文字設置為藍色