iOS中UIButton控制項的用法及部分參數解釋,iosuibutton

來源:互聯網
上載者:User

iOS中UIButton控制項的用法及部分參數解釋,iosuibutton

 

在UI控制項中UIButton是極其常用的一類控制項,它的類對象建立與大多數UI控制項使用執行個體方法init建立不同,通常使用類方法建立:

+ (id)buttonWithType:(UIButtonType)buttonType;

如果使用執行個體方法建立UIButton對象,如:

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)];

對象的建立是沒有任何問題的,但是當為這個button對象設定一個標題時,如:

    [button setTitle:@"asd" forState:UIControlStateNormal];

但程式運行之後螢幕上是沒有任何顯示的,但這並不是程式有bug,只是當button使用執行個體方法建立時,標題文字的預設顏色是白色,在白色螢幕或無設定背景色的螢幕上自然是看不見的。而如果使用類方法建立UIButton對象,則需要輸入一個UIButtonType類型的參數,這是一個枚舉,具體如下:

typedef NS_ENUM(NSInteger, UIButtonType) {

    UIButtonTypeCustom = 0,                         // 自訂類型,即無類型,當選擇這個參數市與使用類方法效果相同

    UIButtonTypeSystem,                               // 標準類型,一般情況下地選擇,在iOS7.0版本開始使用

    UIButtonTypeDetailDisclosure,                  //與標準類型相比多了一個藍色的information標誌

    UIButtonTypeInfoLight,                            //information的標誌亮一點

    UIButtonTypeInfoDark,                             //information的標誌暗一點

    UIButtonTypeContactAdd,                         //與標準類型相比多了一個加號標誌

    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // 不贊成使用,已被標準類型代替

};

除了UIButtonTypeCustom以外其他枚舉類型的預設顏色都是藍色,幾時在button上放置圖片,最初的現實效果也是藍色。

而自訂類型則可正確顯示圖片本來的顏色。此外,若同時為button設定圖片和文字,則圖片在左文字在右。

設定標題文字:- (void)setTitle:(NSString *)title forState:(UIControlState)state;

設定圖片:- (void)setImage:(UIImage *)image forState:(UIControlState)state;

作用是在狀態state下顯示字串對象title,圖片image,其中如果要在不同狀態顯示不同圖片則需要圖片相同大小,否則會錯誤顯示。

其中UIControlState是枚舉類型,具體如下:

typedef NS_OPTIONS(NSUInteger, UIControlState) {

    UIControlStateNormal       = 0,                          //正常狀態

    UIControlStateHighlighted  = 1 << 0,                  // 高亮狀態,當button被點擊時會從正常狀態轉變為高亮狀態,鬆開時自動轉化為正常轉態

    UIControlStateDisabled     = 1 << 1,                   //不可用狀態,通過屬性enabled設定

    UIControlStateSelected     = 1 << 2,                  // 選中轉態,通過屬性selected設定

    UIControlStateApplication  = 0x00FF0000,              // 當應用程式標誌時

    UIControlStateReserved     = 0xFF000000               //為內部架構預留

};

 常見的用法是:

[button setTitle:title1 forState:UIControlStateNormal];

[button setTitle:title2 forState:UIControlStateHighlighted];

這樣button的正常狀態顯示title1,按下時顯示title2,鬆開時顯示title1.

設定圖片同理。

設定背景圖片:- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state

與設定圖片不同,背景圖片顯示在文字下方,而不是左邊。

 

設定標題文字顏色:- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state

此方法只改變標題文字顏色,不影響圖片顏色,而屬性tintColor則同時影響標題文字和圖片的顏色,但不影響背景映像顏色。另外,當UIButton建立時選擇UIButtonTypeCustom或使用執行個體方法建立時屬性tintColor無效。

 

 設定響應事件:- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

意思是當controlEvents發生時target調用action方法,用OC的說法就是發送action訊息給target對象。

其中controlEvents是枚舉類型,具體如下:

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {

    UIControlEventTouchDown           = 1 <<  0,      // 按鈕按下事件

    UIControlEventTouchDownRepeat     = 1 <<  1,      // 雙擊或多次點擊事件

    UIControlEventTouchDragInside     = 1 <<  2,        //控制項視窗內部拖動事件

    UIControlEventTouchDragOutside    = 1 <<  3,      //控制項視窗外部拖動事件

    UIControlEventTouchDragEnter      = 1 <<  4,       //從控制項視窗外部拖動進入內部事件

    UIControlEventTouchDragExit       = 1 <<  5,        //控制項視窗外部拖動進入內部事件

    UIControlEventTouchUpInside       = 1 <<  6,       //控制項視窗內部點擊抬起事件,發生在UIControlEventTouchDown事件之後

    UIControlEventTouchUpOutside      = 1 <<  7,     //控制項視窗內部點擊視窗外部抬起事件

    UIControlEventTouchCancel         = 1 <<  8,       //所有點擊取消事件,如:太多手指,上鎖,或有電話進入

 

    UIControlEventValueChanged        = 1 << 12,     // 控制項值改變事件,多用於UISlider,UISegmentControl,UIPageControl

 

    UIControlEventEditingDidBegin     = 1 << 16,     // UITextField開始編輯事件,即進入第一響應時

    UIControlEventEditingChanged      = 1 << 17,    //UITextField中的文字改變時

    UIControlEventEditingDidEnd       = 1 << 18,     //UITextField結束編輯時,即取消第一響應時

    UIControlEventEditingDidEndOnExit = 1 << 19,     // 點擊'return key'時,預設是換行鍵,會自動取消第一響應結束編輯

 

    UIControlEventAllTouchEvents      = 0x00000FFF,  // 所有觸摸事件

    UIControlEventAllEditingEvents    = 0x000F0000,  // 所有 UITextField編輯事件

    UIControlEventApplicationReserved = 0x0F000000,  // range available for application use

    UIControlEventSystemReserved      = 0xF0000000,  // range reserved for internal framework use

    UIControlEventAllEvents           = 0xFFFFFFFF        //所有事件

};

 

取消響應事件:- (void)removeTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

作用是取消當controlEvents發生時target對action的響應,即當事件發生時target不再觸發方法action,當然前提是之前已經添加了對這個事件的方法響應,其中參數均可設定為nil,表示任意、所有,即移除全部。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.