第1、UIButton的定義app
UIButton *button=[[UIButton buttonWithType:(UIButtonType);框架
可以定義的button類型有如下6種,
typedef enum {
UIButtonTypeCustom = 0, 自定義風格ide
UIButtonTypeRoundedRect, 圓角矩形 atom
UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要作詳細說明用spa
UIButtonTypeInfoLight, 亮色感嘆號
UIButtonTypeInfoDark, 暗色感嘆號
UIButtonTypeContactAdd, 十字加號按鈕orm
} UIButtonType;事件
第2、設置frame圖片
button1.frame = CGRectMake(20, 20, 280, 40);rem
[button setFrame:CGRectMake(20,20,50,50)];get
第3、button背景色
button1.backgroundColor = [UIColor clearColor];
[button setBackgroundColor:[UIColor blueColor]];
第4、state狀態
forState: 這個參數的做用是定義按鈕的文字或圖片在何種狀態下才會顯現
enum {
UIControlStateNormal = 0, 常規狀態顯現
UIControlStateHighlighted = 1 << 0, 高亮狀態顯現
UIControlStateDisabled = 1 << 1, 禁用的狀態纔會顯現
UIControlStateSelected = 1 << 2, 選中狀態
UIControlStateApplication = 0x00FF0000, 當應用程序標誌時
UIControlStateReserved = 0xFF000000 爲內部框架預留,能夠無論他
};
@property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently
@property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application
@property(nonatomic,getter=isHighlighted)BOOL highlighted;
第五 、設置button填充圖片和背景圖片
[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
[buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
第6、設置button標題和標題顏色
[button1 setTitle:@"點擊" forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
第7、設置按鈕按下會發光
button.showsTouchWhenHighlighted=NO;
第8、添加或刪除事件處理
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
第9、 設置按鈕內部圖片間距和標題間距
UIEdgeInsets insets; // 設置按鈕內部圖片間距 insets.top = insets.bottom = insets.right = insets.left = 10; bt.contentEdgeInsets = insets; bt.titleEdgeInsets = insets; // 標題間距