標籤:
// 1.建立一個自訂的按鈕
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
// 2.添加按鈕
[self.view addSubview:btn];
// 3.設定按鈕的位置和尺寸
btn.frame = CGRectMake(100, 100, 100, 100);
// 4.監聽按鈕點擊(點擊按鈕後就會調用self的btnClick方法)
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
// 5.設定按鈕在預設狀態下的屬性
// 5.1.預設狀態的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_01"] forState:UIControlStateNormal];
// 5.2.預設狀態的文字
[btn setTitle:@"點我啊" forState:UIControlStateNormal];
// 5.3.預設狀態的文字顏色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 6.設定按鈕在高亮狀態下的屬性
// 6.1.高亮狀態的背景
[btn setBackgroundImage:[UIImage imageNamed:@"btn_02"] forState:UIControlStateHighlighted];
// 6.2.高亮狀態的文字
[btn setTitle:@"摸我幹啥" forState:UIControlStateHighlighted];
// 6.3.高亮狀態的文字顏色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
IOS學習--UIButton常用方法(20150122)