UIButton的使用,UIButton使用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];
_window.backgroundColor = [UIColorwhiteColor];
[_windowmakeKeyAndVisible];
/*----------------------UIButton的使用------------------------*/
// //建立按鈕
//如果用這種方法,無法設定類型
// UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 50, 100, 50)];
// //設定按鈕的類型
// button.buttonType = UIButtonTypeRoundedRect;
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
//設定顯示的地方和大小
button.frame = CGRectMake(100, 50, 100, 50);
//設定背景顏色
// button.backgroundColor = [UIColor redColor];
//設定標題,設定標題一定要綁定狀態
//設定普通狀態顯示的標題
[button setTitle:@"按鈕"forState:UIControlStateNormal];
//設定點擊(高亮)狀態顯示的標題
[button setTitle:@"點我了"forState:UIControlStateHighlighted];
//設定標題的顏色
[button setTitleColor:[UIColorredColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColorgreenColor] forState:UIControlStateHighlighted];
//按鈕標題字型
button.titleLabel.font = [UIFontsystemFontOfSize:15];
//文本位置
button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//位移量
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
//設定按鈕的圓角屬性
button.layer.cornerRadius =5;
//添加按鈕的點擊事件
//UIControlEventTouchDown:按鈕點擊下的那一個事件就被觸發了
//UIControlEventTouchDownRepeat按鈕被多次點下的時候執行事件
//UIControlEventTouchDragInside 按鈕被按著拖拽的時候被執行
//UIControlEventTouchUpInside 按鈕被點擊後鬆開 執行事件,常用的
[button addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
//設定button的tag值
button.tag = 1;
//添加到視圖上顯示
[_window addSubview:button];
//建立一個系統樣式的按鈕
UIButton *button1 = [UIButtonbuttonWithType:UIButtonTypeContactAdd];
button1.frame = CGRectMake(100, 100, 50, 50);
button1.tag = 2;
//添加一個點擊事件
[button1 addTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:button1];
/*-----------------------自訂按鈕的使用-----------------------*/
//建立一個按鈕
UIButton *customButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
//設定frame
customButton.frame = CGRectMake(220, 200, 80, 40);
//建立兩個圖片
UIImage *image1 = [UIImageimageNamed:@"back_on_black.png"];
UIImage *image2 = [UIImageimageNamed:@"back_on.png"];
//設定button顯示的圖片,則按鈕的標題不會出現了
//http://blog.csdn.net/u012762130/article/details/42591983
// [customButton setImage:image1 forState:UIControlStateNormal];
// [customButton setImage:image2 forState:UIControlStateHighlighted];
//禁用按鈕,此時按鈕的圖片顏色會變淺
// button.enabled = NO;
//如果想顯示圖片還顯示標題,則用一下方法
[customButton setBackgroundImage:image1forState:UIControlStateNormal];
[customButton setBackgroundImage:image2forState:UIControlStateHighlighted];
//設定標題
[customButton setTitle:@"按鈕"forState:UIControlStateNormal];
[customButton setTitle:@"摸我了"forState:UIControlStateHighlighted];
button.userInteractionEnabled =NO;//互動事件
//設定點擊事件
[customButton addTarget:selfaction:@selector(customAction)forControlEvents:UIControlEventTouchUpInside];
[_window addSubview:customButton];
return YES;
}
- (void)customAction {
NSLog(@"點擊了");
}
//按鈕的點擊事件
- (void)buttonAction:(UIButton *)button {
if (button.tag ==1) {
NSLog(@"button被點了");
}else if (button.tag ==2) {
NSLog(@"button1被點了");
}
}