UIButton 按鈕
屬性
- 建立
CGRect rect = CGRectMake(,,,);
UIButton *button = [[UIButton alloc]initWithFrame: rect];
- buttonType————按鍵的風格
UIButton *button = [UIButton buttonWithType:(buttonType)];
UIButtonTypeCustom 自定義風格
UIButtonTypeRoundedRect 圓角矩形
UIButtonTypeDetailDisclosure 藍色小箭頭
UIButtonTypeInfoLight 亮色感歎号
UIButtonTypeInfoDark 暗色感歎号
UIButtonTypeContactAdd 十字加号按鈕
- backgroundColor————button的背景顔色
[button setBackgroundColor:[UIColor redColor]];
- state————forState:這個參數的作用是定義按鈕的文字或圖檔在何種狀态下才會顯現
UIControlStateNormal = 0; 正常狀态顯現
UIControlStateHighlighted = 1<<0; 高亮狀态顯現
UIControlStateDisabled = 1<<1; 禁用的狀态才會顯現
UIControlStateSelected = 1<<2; 選中狀态
UIControlStateApplication = 0x00FF0000; 當應用程式标志時
UIControlStateReserved = 0xFF00000; 為内部架構預留
方法
- 設定button标題和标題顔色
[button setTitle:@"" forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- 設定button的填充圖檔和背景圖檔
[button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
- 設定按鈕按下會發光
- 添加或删除事件處理
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; //添加
[button removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside]; //删除
forControlEvents:
UIControlEventTouchDown 單點觸摸按下事件:使用者點觸螢幕,或者又有新手指落下的時候
UIControlEventTouchDownRepeat 多點觸摸按下事件,點觸計數大于1,使用者按下第二,三,四根手指的時候
UIControlEventTouchDragInside 當一次觸摸在控件視窗内拖動時
UIControlEventTouchDragEnter 當一次觸摸從控件視窗之外拖動到内部時
UIControlEventTouchDragExit 當一次觸摸從控件視窗之外拖動到外部時
UIControlEventTouchCancel 所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷
UIControlEventTouchUpInside 點選擡起時
UIControlEventTouchOutside 點選下去時
UIControlEventValueChanged 值改變
UIControlEventEditingDidBegin 當文本控件中的文本編輯時發送通知
UIControlEventEdingChanged 當文本控件中的文本被改變發送通知
UIControlEventEditingDidEnd 當文本控件中編輯結束時發送通知
UIControlEventEditingDidOnExit 當文本控件内通過按下Enter鍵結束編輯時發送通知
UIControlEventAlltouchEvents 通知所有觸摸事件
UIControlEventAllEditingEvents 通知所有關于文本編輯的事件
UIControlEventAllEvents 通知所有事件
- 定義按鈕标題字型格式
[button.titleLabell setFont:[UIFont systemFontOfSize:]];
- 設定按鈕内部圖檔間距和标題間距
UIEdgeInsets insets;
insets.top = insets.bottom = insets.right = insets.left = ;
button.contentEdgeInsets = insets;
button.titleEdgeInsets = insets; //标題間距
- 給button設定标簽,用來辨識點選的是哪個button,常用在委托方法中
- 設定圓角
[button.layer setMasksTobounds:YES]; 邊緣是否剪切
[button.layer setCornerRadius:]; 圓角
[button.layer setBorderWidth:]; 邊框寬度
[button.layer setbBorderColor:[[UIColor redColor]CGColor]]; 邊框顔色