1. 程式人生 > >IOS--UIButton的使用方法詳細

IOS--UIButton的使用方法詳細

   // UIButton的常用方法

    UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom]; // 初始化時設定Button樣式

    // 風格有如下

//    typedef enum {

//        UIButtonTypeCustom = 0,           // 自定義,無風格

//        UIButtonTypeRoundedRect,        // 白色圓角矩形,類似偏好設定表格單元或者地址簿卡片

//        UIButtonTypeDetailDisclosure,//藍色的披露按鈕,可放在任何文字旁

//        UIButtonTypeInfoLight,//微件(widget)使用的小圓圈資訊按鈕,可以放在任何文字旁

//        UIButtonTypeInfoDark,//白色背景下使用的深色圓圈資訊按鈕

//        UIButtonTypeContactAdd,//藍色加號(+)按鈕,可以放在任何文字旁

//    } UIButtonType;

    

    

    // 最常用

    oneButton.frame = CGRectMake(10, 10, 300, 30); // 設定oneButton的位置和大小

    oneButton.backgroundColor = [UIColor blueColor]; // 設定oneButton背景色

    oneButton.alpha = 0.8; // 設定oneButton的透明度範圍在0.0-1.0之間

    [oneButton setTitle:@"我是一個UIButton" forState:UIControlStateNormal]; // 設定在什麼狀態下顯示什麼文字

    [oneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 設定什麼狀態下字型什麼顏色

    [oneButton setBackgroundImage:[UIImage imageNamed:@"image.jpg"] forState:UIControlStateNormal]; // 設定什麼狀態下顯示的背景影象

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

//    enum {

//        UIControlStateNormal       = 0,//常態

//        UIControlStateHighlighted  = 1 << 0,    //  高亮

//        UIControlStateDisabled     = 1 << 1,    //禁用

//        UIControlStateSelected     = 1 << 2,    // 選中

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

//        UIControlStateReserved     = 0xFF000000 // 為內部框架預留的

//    };

    oneButton.tag = 10001; // 設定標籤,用於便於點選的是哪個按鈕,常用在委託方法中

    [oneButton.titleLabel setFont:[UIFont systemFontOfSize:25.0f]]; // 設定文字的大小

    

    oneButton.adjustsImageWhenDisabled = NO; // 對按鈕進行微調。當按鈕禁用時,影象會被畫的顏色深一些 預設是YES,禁止設定為NO

    oneButton.adjustsImageWhenHighlighted = NO; // 對按鈕進行微調。當按鈕高亮是,影象會被畫得顏色深一些 預設是YES,禁止設定為NO

    

    oneButton.showsTouchWhenHighlighted = YES; // 設定點選的時候是否有光照得效果

    

    // 給按鈕新增響應事件

    [oneButton addTarget:self action:@selector(oneButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];//當按鈕被按下時會執行oneButtonTouchUpinside:方法。這個方法是自定義的,需要自己寫出。引數是(UIButton *)型別

    

    // 新增到view
    [self.view addSubview:oneButton];