#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
/*
UIButton 按鈕
*/
//1.創(chuàng)建
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(20, 60, 100, 50)];
//2.顯示
[self.view addSubview:button];
//3.屬性
button.backgroundColor = [UIColor lightGrayColor];
//①.按鈕標(biāo)題
[button setTitle:@"一般" forState:UIControlStateNormal];
[button setTitle:@"高亮" forState:UIControlStateHighlighted];
[button setTitle:@"選中" forState:UIControlStateSelected];
//按鈕標(biāo)題字體
button.titleLabel.font = [UIFont systemFontOfSize:30];
//②.按鈕顏色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
//③.按鈕圖片
UIImage *image = [UIImage imageNamed:@"back_on"];
[button setImage:image forState:UIControlStateNormal];
//④.偏移
[button setTitleEdgeInsets:UIEdgeInsetsMake(0, -100, 0, 0)];
//選中狀態(tài):系統(tǒng)默認(rèn)NO
button.selected = NO;
//使用狀態(tài):系統(tǒng)默認(rèn)YES
// button.enabled = NO;// NO --> Disabled
//4.方法
//添加事件
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UIButton *)button{
button.selected = ! button.selected;
NSLog(@"code按鈕點(diǎn)擊");
}
- (IBAction)tap:(id)sender {
/*
id類型
button 按鈕類型
*/
// sender.backgroundColor = [UIColor blackColor];
NSLog(@"xib 文件按鈕點(diǎn)擊");
//改變按鈕的選中狀態(tài)
botton.selected = NO;
//是否響應(yīng)觸摸事件
button.userInteractionEnabled = NO;
}
@end
2016-02-24 21:43:01.605 UIButton[2804:338048] xib 文件按鈕點(diǎn)擊
button //按鈕
Touch Up inside //按下向上的一瞬間状土,用的比較多
Title //標(biāo)題
state //狀態(tài)
selected //已選中,勾選狀態(tài)
屏幕快照 2016-02-24 下午9.42.24.png