標籤:ios開發 uibutton objective-c
// 初始化按鈕並設定類型UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];// 能夠定義的UIButton類型有以下6種:// typedef enum {// UIButtonTypeCustom = 0, 自訂風格// UIButtonTypeRoundedRect, 圓角矩形// UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用// UIButtonTypeInfoLight, 亮色驚嘆號// UIButtonTypeInfoDark, 暗色驚嘆號// UIButtonTypeContactAdd, 十字加號按鈕// } UIButtonType;// 設定按鈕大小和位置btn.frame = CGRectMake(20, 360, 280, 45);// 設定按鈕背景顏色btn.backgroundColor = [UIColor colorWithRed:254/255.0f green:254/255.0f blue:254/255.0f alpha:1.0f];// 設定按鈕文字[btn setTitle:@"Normal" forState:UIControlStateNormal];[btn setTitle:@"Pressed" forState:UIControlStateHighlighted];// forState這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現,以下是幾種狀態:// enum {// UIControlStateNormal = 0, 常規狀態顯現// UIControlStateHighlighted = 1 << 0, 高亮狀態顯現// UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現// UIControlStateSelected = 1 << 2, 選中狀態// UIControlStateApplication = 0x00FF0000, 當應用程式標誌時// UIControlStateReserved = 0xFF000000 為內部架構預留,可以不管他// };// 設定按鈕文字顏色[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];// 設定按鈕文字字型[btn.titleLabel setFont:[UIFont systemFontOfSize:17]];[btn.layer setMasksToBounds:YES];// 設定按鈕四個圓角半徑[btn.layer setCornerRadius:4.0];// 設定按鈕邊框寬度[btn.layer setBorderWidth:0.5];// 設定按鈕邊框顏色CGColorRef colorref = CGColorCreate(CGColorSpaceCreateDeviceRGB(),(CGFloat[]){168/255.0f, 168/255.0f, 168/255.0f, 1.0});[btn.layer setBorderColor:colorref];// 去除按鈕在疊加視圖中的按下延遲tableView.delaysContentTouches = NO;// 添加點擊事件[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];// 在視圖中顯示按鈕[tableView addSubview:btn];// 按鈕點擊事件- (void)btnAction:(id)sender{ // do something}
本文固定連結:http://www.itechzero.com/coding/objective-c/ios-development-series-one-uibutton-usage-summary/,轉載請註明出處。