第一、UIButton的定義
UIButton *button=[[UIButton buttonWithType:(UIButtonType);
能夠定義的button類型有以下6種,
typedef enum {
UIButtonTypeCustom = 0, 自定義風(fēng)格
UIButtonTypeRoundedRect, 圓角矩形
UIButtonTypeDetailDisclo
UIButtonTypeInfoLight, 亮色感嘆號
UIButtonTypeInfoDark, 暗色感嘆號
UIButtonTypeContactAdd, 十字加號按鈕
} UIButtonType;
第二、設(shè)置frame
button1.frame = CGRectMake(20, 20, 280, 40);
[button setFrame:CGRectMake(20,20,50,50)];
第三、button背景色
button1.backgroundColor = [UIColor clearColor];
[button setBackgroundColor:[UIColor blueColor]];
第四、state狀態(tài)
forState: 這個參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會顯現(xiàn)
enum {
UIControlStateNormal = 0, 常規(guī)狀態(tài)顯現(xiàn)
UIControlStateHighlighte
UIControlStateDisabled = 1 << 1, 禁用的狀態(tài)才會顯現(xiàn)
UIControlStateSelected = 1 << 2, 選中狀態(tài)
UIControlStateApplicatio
UIControlStateReserved = 0xFF000000 為內(nèi)部框架預(yù)留,可以不管
};
@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;
第五 、設(shè)置button填充圖片和背景圖片
[buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
[buttonsetBackgroundImage
第六、設(shè)置button標(biāo)題和標(biāo)題顏色
[button1 setTitle:@"點擊" forState:UIControlStateNormal];
[buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
第七、設(shè)置按鈕按下會發(fā)光
button.showsTouchWhenHighlighte
第八、添加或刪除事件處理
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpIns
[btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpIns
第九、 設(shè)置按鈕內(nèi)部圖片間距和標(biāo)題間距
UIEdgeInsets insets; // 設(shè)置按鈕內(nèi)部圖片間距
insets.top = insets.bottom = insets.right = insets.left = 10;
bt.contentEdgeInsets = insets;
bt.titleEdgeInsets = insets; // 標(biāo)題間距
十、重寫繪制行為
新聞熱點
疑難解答