iOS之UIButton的使用

來源:互聯網
上載者:User

標籤:outside   gen   equal   name   禁用   大小   範圍   sele   str   

1.建立

1 @property(nonatomic, strong) UIButton *btn;

 

 1 //1.不常用 2     self.btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 200, 50)]; 3     //2.常用這種方法(上面那種可捨棄不用) 4     self.btn = [UIButton buttonWithType:UIButtonTypeCustom]; 5     /* 6      UIButtonTypeCustom = 0,//自訂類型 7      UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),//系統類別型 8       9      UIButtonTypeDetailDisclosure,//詳細描述樣式(圓圈中間加個i)10      UIButtonTypeInfoLight,//淺色的詳細描述樣式11      UIButtonTypeInfoDark,//深色的詳細描述樣式12      UIButtonTypeContactAdd,//加號樣式13      14      UIButtonTypeRoundedRect = UIButtonTypeSystem,//圓角矩形15      };16 17      */

2.設定大小

 _btn.frame = CGRectMake(50, 50, 200, 100);

3.設定背景

1 //設定背景顏色2     [self.btn setBackgroundColor:[UIColor orangeColor]];3     //設定背景圖片4     [self.btn setBackgroundImage:[UIImage imageNamed:@"bg"] forState:UIControlStateNormal];

4.設定標題

/*     註:前四個用的多一點     UIControlStateNormal        //正常狀態下(這個用的最多)     UIControlStateHighlighted   //高亮狀態下(按鈕按下還未抬起的時候)     UIControlStateDisabled      //禁用狀態下     UIControlStateSelected      //選中狀態下          UIControlStateApplication   //當應用程式標誌時     UIControlStateReserved      //為內部架構預留     */        [_btn setTitle:@"編輯" forState:UIControlStateNormal];            //設定標題顏色    [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    

5.給按鈕添加事件

//如果事件有參數 只能是UIButton類型    //當按鈕的一個事件被觸發了,系統會將這個按鈕對象作為參數傳遞給事件    /*     UIControlEventTouchDown  按下去就會響應     UIControlEventTouchUpOutside 點下去沒反應 在該範圍內放手才有響應(一般用這個)     */    [_btn addTarget:self action:@selector(changTitle) forControlEvents:UIControlEventTouchDown];    [_btn addTarget:self action:@selector(changTitle) forControlEvents:UIControlEventTouchUpOutside];    

改變按鈕的標題的方法(3個)

- (void)changTitle:(UIButton *)sender{        //改變按鈕的標題(有三個方法)    //1.property屬性: _btn 或者  self.btn  訪問    //得到按鈕標題    if([_btn.titleLabel.text isEqualToString:@"編輯"]){        [self.btn setTitle:@"完成" forState:UIControlStateNormal];    } else{        [self.btn setTitle:@"編輯" forState:UIControlStateNormal];    };        //2.tag值    UIButton *button = [self.view viewWithTag:1];            //3.作為參數直接傳遞過來(這種方式用的比較多)    //addTarger}

 

iOS之UIButton的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.