創建兩種方法:(1)常規的initWithFrame的方式 UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 50, 100, 75)];[btn1 setTitle:@"close" forState:UIControlStateNormal];btn1.backgroundColor = [UIColor greenColor];//button的背景顏色
[btn1 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];//button的背景圖片
(2)UIButton 的一個類方法(也可以說是靜態方法)buttonWithType UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn2.frame = CGRectMake(200, 20, 50, 60);btn2.backgroundColor = [UIColor blackColor];[btn2 setTitle:@"clicke" forState:UIControlStateNormal];[self.window addSubview:btn1];[self.window addSubview:btn2]; 能夠定義的button類型由6種typedef enum { UIButtonTypeCustom = 0, // 自定義,無風格
UIButtonTypeRoundedRect, // 白色圓角矩形,類似偏好設置表格單元或者地址簿卡片
UIButtonTypeDetailDisclosure, // 藍色的披露按鈕,可放在任何文字旁
UIButtonTypeInfoLight, // 微件(widget)使用的小圓圈信息按鈕,可以放在任何文字旁
UIButtonTypeInfoDark, // 白色背景下使用的深色圓圈信息按鈕
UIButtonTypeContactAdd, // 藍色加號(+)按鈕,可以放在任何文字旁
} UIButtonType; [btn1 setTitle:@"BTN1" forState:UIControlStateNormal]; //設置按鈕的標題
[btn1 setTitle:@"高亮狀態" forState:UIControlStateHighlighted]; //高亮狀態按鈕title值
[btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal]; //你也可以為按鈕的某一狀態設置為圖
[btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; //設置標題顏色
[btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ]; //陰影
[btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted]; //背景圖像
btn1.titleLabel.font = [UIFont fontWithName:@“test” size:18]; //設置按鈕字體大小
[btn1 setTag:101] ; //設置tag值btn1.layer.cornerRadius = 4.5; //設置圓角——四個圓角半徑 btn1.layer.borderWidth = 0.5; // 按鈕邊框寬度
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // 設置顏色空間為rgb,用于生成ColorRef
CGColorRef borderColorRef = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 1 }); // 新建一個紅色的ColorRef,用于設置邊框(四個數字分別是 r, g, b, alpha)
參數 forState . 這個參數決定了標題、圖像或其他屬性將在何種狀態下顯現。你可以編程令按鈕在那個狀態變化 enum { UIControlStateNormal = 0, // 常態
UIControlStateHighlighted = 1 << 0, // 高亮
UIControlStateDisabled = 1 << 1, // 禁用
UIControlStateSelected = 1 << 2, // 選中
UIControlStateApplication = 0x00FF0000, // 當應用程序標志使用時
UIControlStateReserved = 0xFF000000 // 為內部框架預留的
}; typedef NSUInteger UIControlState; 你只要掌握前四種狀態就好了。當按鈕高亮或者禁用,UIButton 類可以調整自己的外觀,下面幾個屬性可以讓你按照需要對按鈕的外觀進行微調:adjustsImageWhenHighlighted默認情況下,在按鈕被禁用時,圖像會被畫的顏色深一些。要禁用此功能,請將這個屬性設置為NO:btn1.adjustsImageWhenHighlighted = NO; adjustsImageWhenDisabled默認情況下,按鈕在被禁用時,圖像會被畫的顏色淡一些。要禁用此功能,請將這個屬性設置為NO:btn1.adjustsImageWhenDisabled = NO; showsTouchWhenHighlighted這個屬性設置為YES,可令按鈕在按下時發光。這可以用于信息按鈕或者有些重要的按鈕:btn1.showsTouchWhenHighlighted = YES; 顯示控件[self.view addSubview:btn1]; [self.view addSubview:btn2]; 重寫繪制行為 你可以通過子類化按鈕來定制屬于你自己的按鈕類。在子類化的時候你可以重載下面這些方法,這些方法返回CGRect結構,指明了按鈕每一組成部分的邊界。注意:不要直接調用這些方法, 這些方法是你寫給系統調用的。 backgroundRectForBounds //指定背景邊界
contentRectForBounds // 指定內容邊界
titleRectForContentRect // 指定文字標題邊界
imageRectForContentRect //指定按鈕圖像邊界
例:- (CGRect)imageRectForContentRect:(CGRect)bounds{ return CGRectMake(0.0, 0.0, 44, 44); } [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];//添加點擊按鈕事件
-(void)btnPressed:(id)sender{ UIButton* btn = (UIButton*)sender; //開始寫你自己的動作
}forControlEvents參數類型 typedef NS_OPTIONS(NSUInteger, UIControlEvents) { UIControlEventTouchDown = 1 << 0, // 單點觸摸按下事件:用戶點觸屏幕,或者又有新手指落下的時候。
UIControlEventTouchDownRepeat = 1 << 1, // 多點觸摸按下事件,點觸計數大于1:用戶按下第二、三、或第四根手指的時候。
UIControlEventTouchDragInside = 1 << 2, // 當一次觸摸在控件窗口內拖動時。
UIControlEventTouchDragOutside = 1 << 3, // 當一次觸摸在控件窗口之外拖動時。
UIControlEventTouchDragEnter = 1 << 4, // 當一次觸摸從控件窗口之外拖動到內部時
UIControlEventTouchDragExit = 1 << 5, // 當一次觸摸從控件窗口內部拖動到外部時。
UIControlEventTouchUpInside = 1 << 6, // 所有在控件之內觸摸抬起事件
UIControlEventTouchUpOutside = 1 << 7, // 所有在控件之外觸摸抬起事件(點觸必須開始與控件內部才會發送通知)。
UIControlEventTouchCancel = 1 << 8, //所有觸摸取消事件,即一次觸摸因為放上了太多手指而被取消,或者被上鎖或者電話呼叫打斷。
UIControlEventValueChanged = 1 << 12, // 當控件的值發生改變時,發送通知。用于滑塊、分段控件、以及其他取值的控件。你可以配置滑塊控件何時發送通知,在滑塊被放下時發送,或者在被拖動時發送。
UIControlEventEditingDidBegin = 1 << 16, // 當文本控件中開始編輯時發送通知
UIControlEventEditingChanged = 1 << 17, // 當文本控件中的文本被改變時發送通知。
UIControlEventEditingDidEnd = 1 << 18, // 當文本控件中編輯結束時發送通知。
UIControlEventEditingDidEndOnExit = 1 << 19, // 當文本控件內通過按下回車鍵(或等價行為)結束編輯時,發送通知。
UIControlEventAllTouchEvents = 0x00000FFF, // 通知所有觸摸事件。
UIControlEventAllEditingEvents = 0x000F0000, // 通知所有關于文本編輯的事件。
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents = 0xFFFFFFFF // 通知所有事件
};
posted on 2014-07-15 13:37
王海光 閱讀(6295)
評論(0) 編輯 收藏 引用 所屬分類:
IOS