標籤: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的使用