UIButton常見屬性和方法


一、創建, 兩種方法:

  1. 常規的 initWithFrame

    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];

  2. UIButton 的一個類方法(也可以說是靜態方法)buttonWithType

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    風格有如下:

typedef enum {
UIButtonTypeCustom = 0,           // no button type   自定義,無風格
UIButtonTypeRoundedRect,        // rounded rect, flat white button, like in address card 白色圓角矩形,類似偏好設置表格單元或者地址簿卡片
UIButtonTypeDetailDisclosure,   ///細節展示按鈕,藍色的披露按鈕,可放在任何文字旁
UIButtonTypeInfoLight,              //淺色的信息按鈕,微件(widget)使用的小圓圈信息按鈕,可以放在任何文字旁
UIButtonTypeInfoDark,              //白色背景下使用的深色圓圈信息按鈕
UIButtonTypeContactAdd,         //藍色加號(+)按鈕,可以放在任何文字旁
} UIButtonType;
   二、設置屬性

  1.Frame屬性

  第2種方法創建按鈕后你可以給按鈕的frame屬性賦值,用一個CGRect結構設置他的位置和大小

  CGRect btn2Frame = CGRectMake(10.0, 10.0, 60.0, 44.0);

  btn2.frame =btn2Frame;

  2. title屬性

    [btn1 setTitle:@"BTN1" forState:UIControlStateNormal];

  3.圖片屬性:

    [btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];

    用setImage設置圖片不會填滿button,上下會有空余;用setBackgroundImage會填滿button;

  4.按鈕標題的顏色和陰影,以及按鈕的背景。方法 setTitleColor 和 setTitleShadowColor 都需要一個UIColor對象做參數:

    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//設置標題顏色

    [btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//陰影

    [btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景圖像

    上面幾個方法都提到 共同的參數 forState . 這個參數決定了標題、圖像或其他屬性將在何種狀態下顯現。你可以編程令按鈕在那個狀態變化

enum {
UIControlStateNormal       = 0,  //常態
UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set 高亮
UIControlStateDisabled     = 1 << 1,  //禁用
UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below) 選中
UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use 當應用程序標志使用時
UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use  為內部框架預留的
};
typedef NSUInteger UIControlState;

      選中狀態:button.selected = YES;

      高亮狀態:當點擊button時;

  5.1>修改按鈕上的字體的大小:  btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];

   2>讓UIButton的title居左對齊:btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

   3>此時文字會緊貼到左邊框,我們想使文字距離做邊框保持10個像素的距離。btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

  6.添加事件:

    1.- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

    這些事件都是基於觸摸、基於值、基於編輯。有如下事件會觸發。

    在點擊按鈕是按鈕是凹下去,然后彈起才觸發起事件,按鈕的狀態有: 

typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
    UIControlEventTouchDown                                         = 1 <<  0,      // on all touch downs, 按下按鈕
    UIControlEventTouchDownRepeat                                   = 1 <<  1,      // on multiple touchdowns (tap count > 1),(單擊不滿足,快速
                                                    點擊兩次調用一次方法,快速點擊三次調用三次方法,n次調用n次方法)
UIControlEventTouchDragInside = 1 << 2,    //在button內拖動時,不斷調用方法. UIControlEventTouchDragOutside = 1 << 3,    //當從button內拖動到button外時,不斷動用方法.(button內時不調用) UIControlEventTouchDragEnter = 1 << 4,    //從控件窗口之外拖動到內部時 UIControlEventTouchDragExit = 1 << 5,    //從控件窗口內部拖動到外部時 UIControlEventTouchUpInside = 1 << 6,   //單擊,在按鈕范圍內松開 UIControlEventTouchUpOutside = 1 << 7,    //單擊,在按鈕外面松開 UIControlEventTouchCancel = 1 << 8,    //所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上
                                                    鎖或者電話呼叫打斷。 UIControlEventValueChanged
= 1 << 12, // sliders, etc.當控件的值發生改變時,發送通知。用於滑塊、分段控件
                                                    、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放
                                                    下時發送,或者在被拖動時發送。
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc. UIControlEventEditingDidBegin = 1 << 16, // UITextField.當文本控件中開始編輯時發送通知 UIControlEventEditingChanged = 1 << 17,   // 當文本控件中的文本被改變時發送通知。 UIControlEventEditingDidEnd = 1 << 18,   // 當文本控件中編輯結束時發送通知。 UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing,當文本控件內通過按下回車鍵(或等
                                                    價行為)結束編輯時,發送通知。
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events,通知所有觸摸事件。 UIControlEventAllEditingEvents = 0x000F0000, // for UITextField.通知所有關於文本編輯的事件。 UIControlEventApplicationReserved = 0x0F000000, // range available for application use UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use UIControlEventAllEvents = 0xFFFFFFFF // 通知所有事件。 };

 

  2.adjustsImageWhenDisabled

    當按鈕禁用的情況下,圖像的顏色會被畫深一點,默認為YES。

  3.adjustsImageWhenHighlighted

    當按鈕高亮的情況下,圖像的顏色會被畫深一點,默認為YES。

  4.showsTouchWhenHighlighted

    button.showsTouchWhenHighlighted=YES;點擊時的閃光效果會被前景圖片遮住中間部分;

  5.contentEdgeInsets

    設置按鈕的內部內容(包含按鈕圖片和標題)離按鈕邊緣上下左右的距離。

   6.使用storyboard設置UIButton的圓角/邊框等

      


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM