iOS-UIButton

繼承關(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;
}

https://github.com/antoniocasero/ACPButton
很好的開源封裝

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市卖氨,隨后出現(xiàn)的幾起案子会烙,更是在濱河造成了極大的恐慌,老刑警劉巖双泪,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件持搜,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡焙矛,警方通過查閱死者的電腦和手機(jī)葫盼,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來村斟,“玉大人贫导,你說我怎么就攤上這事抛猫。” “怎么了孩灯?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵闺金,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我峰档,道長(zhǎng)败匹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任讥巡,我火速辦了婚禮掀亩,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘欢顷。我一直安慰自己槽棍,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布抬驴。 她就那樣靜靜地躺著炼七,像睡著了一般。 火紅的嫁衣襯著肌膚如雪布持。 梳的紋絲不亂的頭發(fā)上豌拙,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音题暖,去河邊找鬼姆蘸。 笑死,一個(gè)胖子當(dāng)著我的面吹牛芙委,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播狂秦,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼灌侣,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了裂问?” 一聲冷哼從身側(cè)響起侧啼,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎堪簿,沒想到半個(gè)月后痊乾,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡椭更,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年哪审,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片虑瀑。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡湿滓,死狀恐怖滴须,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情叽奥,我是刑警寧澤扔水,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站朝氓,受9級(jí)特大地震影響魔市,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜赵哲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一待德、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧誓竿,春花似錦磅网、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至毙死,卻和暖如春燎潮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背扼倘。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工确封, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人再菊。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓爪喘,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親纠拔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子秉剑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • http://www.reibang.com/p/01c33a4f0ee0
    油菜花花花花閱讀 244評(píng)論 0 0
  • 對(duì)象繼承關(guān)系 UIButton 類本身定義繼承 UIControl ,描述了在 iOS 上所有用戶界面控件的常見基...
    獨(dú)木舟的木閱讀 3,741評(píng)論 0 3
  • 在iOS開發(fā)中經(jīng)常會(huì)涉及到觸摸事件稠诲。本想自己總結(jié)一下侦鹏,但是遇到了這篇文章,感覺總結(jié)的已經(jīng)很到位臀叙,特此轉(zhuǎn)載略水。作者:L...
    WQ_UESTC閱讀 6,024評(píng)論 4 26
  • 三毛在《萬水千山走遍》中寫到高原反應(yīng),蓋丘亞語叫做“索諾奇”:——“那種索諾奇的痛劝萤,沒有身受過的人渊涝,除非拿斧頭去劈...
    C小姐想太多閱讀 1,967評(píng)論 2 2
  • 你一定遇到過這樣的理發(fā)師 “幫我剪短一點(diǎn)點(diǎn),就一點(diǎn)點(diǎn)∈簧停” 然后你的頭發(fā)就短了一大截炸卑,一!大!截!
    桑爾祺一閱讀 275評(píng)論 0 0