UIButton 詳解

2022-08-05 12:15:11 字數 1482 閱讀 7566

1 uibutton *bt = [uibutton buttonwithtype:uibuttontypecontactadd];//

uibuttontypecustom, uibuttontyperoundedrect;

//2 [bt settitle:@"

mybutton

" forstate:uicontrolstatenormal]; //

普通狀態

3 [bt settitle:@"

button1

" forstate:uicontrolstatehighlighted]; //

觸控時4 [bt settitle:@"

disbutton

" forstate:uicontrolstatedisabled]; //

無效時5 [bt settitlecolor:[uicolor redcolor] forstate:uicontrolstatenormal]; //

普通狀態下的字型顏色

67 bt.titlelabel.font = [uifont boldsystemfontofsize:24]; //

按鈕字型

8 bt.titlelabel.shadowoffset = cgsizemake(1, 1

);9 bt.titlelabel.shadowcolor =[uicolor redcolor];

1011

12 [bt setimage:[uiimage imagenamed:@"

normal.png

"] forstate:uicontrolstatenormal];

13//

普通狀態下,只在uibuttontypecustom,uibuttontyperoundedrect下有效

14 [bt setbackgroundimage:[uiimage imagenamed:@"

background.png

"] forstate:uicontrolstatenormal]; //

設定背景圖

1516 uiedgeinsets insets; //

設定按鈕內部間距

17 insets.top = insets.bottom = insets.right = insets.left = 10

;18 bt.contentedgeinsets =insets;

1920 bt.titleedgeinsets = insets; //

標題間距

2122

[bt addtarget:self

23action:@selector(click:)

24 forcontrolevents:uicontroleventtouchupinside]; //

新增事件響應

UIButton

學習用。摘自 1 uibutton類繼承自uicontrol,而uicontrol繼承自uiview,因為uiview就是個矩形區域,所以uibutton例項化的物件其實都是一個矩形,雖然有各種圓角 增加聯絡人 資訊按鈕等等,給它們加個背景它們就現形成矩形了,而且它們有個frame屬性,這就是設定位...

ios UIButton

單點觸控按下事件 使用者點觸螢幕,或者又有新手指落下的時候。 uicontroleventtouchdownrepeat 多點觸控按下事件,...

AutoLayout與UIbutton

1 uibutton貼近右邊,高度固定,寬度跟隨標題變化 class testbutton uibutton class viewcontr...