繼承關(guān)系:
inherits from: UIControl : UIView : UIResponder : NSObject
UIButton 其實(shí)包含 UIImageView 和 UILabel 兩個(gè)控件厘惦,UIButton繼承于UIControl心傀,所以有addtarget監(jiān)聽事件
1炬太、類型:
//初始化一個(gè)按鈕對(duì)象
////////通過類方法來創(chuàng)建 buttonWithType : 類名+方法名
//內(nèi)存自己管理 不能通過alloca init 來創(chuàng)建
// 創(chuàng)建 圓角矩形 的按鈕 (現(xiàn)已扁平化)
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// button 類型 有以下 6 種,
// typedef enum {
// UIButtonTypeCustom = 0, 自定義風(fēng)格
// UIButtonTypeRoundedRect, 圓角矩形
// UIButtonTypeDetailDisclosure, 藍(lán)色小箭頭按鈕隆敢,主要做詳細(xì)說明用
// UIButtonTypeInfoLight, 亮色感嘆號(hào)
// UIButtonTypeInfoDark, 暗色感嘆號(hào)
// UIButtonTypeContactAdd, 十字加號(hào)按鈕
// } UIButtonType;
2、位置
//設(shè)置 button在 view 上的 位置崔慧、尺寸
button1.frame = CGRectMake(20, 20, 280, 20);
3拂蝎、顏色、邊框效果惶室、邊框顏色
//背景顏色
button1.backgroundColor = [UIColor clearColor];
//前景顏色
button1.tintColor = [UIColor redColor];
//圓角 外邊框效果
[Button1.layer setMasksToBounds:YES];
[Button1.layer setCornerRadius:10.0]; //設(shè)置矩形四個(gè)圓角半徑
[Button1.layer setBorderWidth:1.0]; //邊框?qū)挾? (若設(shè)置button為圓形温自,則setCornerRadius的值為button的半徑)
//第一種 邊框顏色
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 1, 0, 0, 1 });
[Button1.layer setBorderColor:colorref];//邊框顏色
//第二種
Button1.layer.backgroundColor = (__bridge CGColorRef)([self colorFromHexRGB:TopSliderColor]);
//第三種
Button1.layer.borderColor=[UIColor grayColor].CGColor;
//導(dǎo)入(QuartzCore.framework)
4、圖片
//設(shè)置button填充
UIImage* image = [UIImage imageNamed:@"btng.png"];
[button setImage:image forState:UIControlStateNormal];
//背景圖片
[button setBackgroundImage:image forState:UIControlStateNormal];
//拿到當(dāng)前圖片
image = button.currentImage;
image = button.currentBackgroundImage;
//若image的寬高小于button的寬高皇钞,則setImage:會(huì)顯示image實(shí)際寬高悼泌,而setBackgroundImage:會(huì)填充button。
//按鈕圖片大小
//1夹界、圖片命名@2x.png
//2馆里、[UIImage imageNamed:@"aa.png"] 圖片會(huì)根據(jù)按鈕的大小改變,而[UIImage imageWithContentsOfFile:imagePath] 真實(shí)顯示圖片大小
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"search-B@2x" ofType:@"png"];
5可柿、標(biāo)題文字 大小鸠踪、字體、狀態(tài)复斥、對(duì)齊营密、顏色
//設(shè)置button 標(biāo)簽文字
[button1 setTitle:@"點(diǎn)擊" forState:UIControlStateNormal];
//取得title
NSString* title = button.titleLabel.text;
title = button.currentTitle;
//設(shè)置title顏色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
//取得title的顏色
UIColor* color = button.currentTitleColor;
//字體、大小
button1.titleLabel.font = [UIFont fontWithName:@"Arial" size:18.0];
//文字陰影
[button1 setTitleShadowColor:[UIColor greenColor] forState:UIControlStateNormal];
[[button1 titleLabel] setShadowOffset:CGSizeMake(1, 1)];
/* forState: 這個(gè)參數(shù)的作用是定義按鈕的文字或圖片在何種狀態(tài)下才會(huì)顯現(xiàn)*/
//以下是幾種狀態(tài)
// enum {
// UIControlStateNormal = 0, 常規(guī)狀態(tài)顯現(xiàn)
// UIControlStateHighlighted = 1 << 0, 高亮狀態(tài)顯現(xiàn)
// UIControlStateDisabled = 1 << 1, 禁用的狀態(tài)才會(huì)顯現(xiàn)
// UIControlStateSelected = 1 << 2, 選中狀態(tài)
// UIControlStateApplication = 0x00FF0000, 當(dāng)應(yīng)用程序標(biāo)志時(shí)
// UIControlStateReserved = 0xFF000000 為內(nèi)部框架預(yù)留目锭,可以不管他
// };
/*
* 默認(rèn)情況下评汰,當(dāng)按鈕高亮的情況下纷捞,圖像的顏色會(huì)被畫深一點(diǎn),如果這下面的這個(gè)屬性設(shè)置為no被去,
* 那么可以去掉這個(gè)功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情況一樣主儡,默認(rèn)情況下,當(dāng)按鈕禁用的時(shí)候编振,圖像會(huì)被畫得深一點(diǎn)缀辩,設(shè)置NO可以取消設(shè)置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的這個(gè)屬性設(shè)置為yes的狀態(tài)下,按鈕按下會(huì)發(fā)光*/
button1.showsTouchWhenHighlighted = YES;
//水平文字對(duì)齊- 居左居右
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
//垂直對(duì)齊方式
[button1 setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
//內(nèi)部?jī)?nèi)容邊距
[button1 setContentEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
//文字內(nèi)在距離
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
//文字自適應(yīng)
//計(jì)算UIlabel寬度踪央,然后在設(shè)置UIButton寬度
6臀玄、事件相應(yīng)
/*
給button添加點(diǎn)擊事件,事件有很多種畅蹂,下面這個(gè)事件的意思是
按下按鈕健无,并且手指離開屏幕的時(shí)候觸發(fā)這個(gè)事件,跟web中的click事件一樣液斜。
觸發(fā)了這個(gè)事件以后累贤,執(zhí)行butClick:這個(gè)方法,addTarget:self 的意思是說少漆,這個(gè)方法在本類中臼膏,也可以傳入其他類的指針
*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
//添加一個(gè)方法 來相應(yīng)按鈕的點(diǎn)擊時(shí)間
- (void)buttonClick:(UIButton*)button{
//父視圖通過tag值獲取子視圖的指針對(duì)象
/*
子視圖可以設(shè)置一個(gè)tag值,然后添加到父視圖上示损,父視圖就可以通過這個(gè)tag值拿到子視圖的指針渗磅。
tag值也可以保存一些用戶的信息。
*/
UILabel* label = (UILabel*)[self.window viewWithTag:100];
label.text = @"我被修改了";
}
7检访、顯示控件
//添加button到當(dāng)前窗口的根視圖
[self.view addSubview:button1];
問題解答
我設(shè)置的圖片為什么變成藍(lán)色 始鱼?
**
//改button的type為: UIButtonTypeCustom
**實(shí)例一:仿 iPhone通話界面按鈕 **
150145_lTAp_1451688.png.jpeg
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(50, 100, 50, 50);
[button setImage:[UIImage imageNamed:@"dial_mute"] forState:UIControlStateNormal];
button.layer.borderColor = [UIColor whiteColor].CGColor;
button.layer.borderWidth = 1.0;
[button.layer setMasksToBounds:YES];
[button.layer setCornerRadius:25.0];//為button的半徑,就是寬或高的一半
實(shí)例二: 選擇框 checkbox
說明:根據(jù)UIControlState 脆贵、 selected 來切換圖片医清,記住是否選中狀態(tài)
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(150, 150, 20, 20);
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
[button setImage:[UIImage imageNamed:@"checkbox_checkedunable"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"checkbox_checked"] forState:UIControlStateSelected];
[self.view addSubview:button];
- (void)buttonClick:(UIButton *)button{
if (button.selected) {
button.selected = NO;
}else{
button.selected = YES;
}
}
- (BOOL)isRemember{
return _button.selected;
}
實(shí)例三:UIButton 圖片的大小
- (UIImage*)transformWidth:(CGFloat)width
height:(CGFloat)height image:(NSString *)imageName {
CGFloat destW = width;
CGFloat destH = height;
CGFloat sourceW = width;
CGFloat sourceH = height;
UIImage *image = [UIImage imageNamed:imageName];
CGImageRef imageRef = image.CGImage;
CGContextRef bitmap = CGBitmapContextCreate(NULL,
destW,
destH,
CGImageGetBitsPerComponent(imageRef),
4*destW,
CGImageGetColorSpace(imageRef),
(kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *resultImage = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap);
CGImageRelease(ref);
return resultImage;
}