天天看點

IOS學習——UIButton

一、建立

兩種方法:

1. 正常的 initWithFrame

C代碼  

  1. UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];  

對代碼建立View(UIControl繼承自UIView,是以也是view)不甚了解的請參看:《有關View的幾個基礎知識點》

2. UIButton 的一個類方法(也可以說是靜态方法)buttonWithType

C代碼  

  1. UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];  

風格有如下

C代碼  

  1. typedef enum {  
  2.     UIButtonTypeCustom = 0,           // no button type   自定義,無風格  
  3.     UIButtonTypeRoundedRect,          // rounded rect, flat white button, like in address card 白色圓角矩形,類似偏好設定表格單元或者位址簿卡片  
  4.     UIButtonTypeDetailDisclosure,//藍色的披露按鈕,可放在任何文字旁  
  5.     UIButtonTypeInfoLight,//微件(widget)使用的小圓圈資訊按鈕,可以放在任何文字旁  
  6.     UIButtonTypeInfoDark,//白色背景下使用的深色圓圈資訊按鈕  
  7.     UIButtonTypeContactAdd,//藍色加号(+)按鈕,可以放在任何文字旁  
  8. } UIButtonType;  

二、設定屬性

1.Frame屬性

第2種方法建立按鈕後你可以給按鈕的frame屬性指派,用一個CGRect結構設定他的位置和大小

C代碼  

  1. CGRect btn2Frame = CGRectMake(10.0, 10.0, 60.0, 44.0);  
  2.     btn2.frame =btn2Frame;  

2. title屬性

對于任何特定狀态下的按鈕,都可以設定該按鈕該狀态下的按鈕标題。用setTitle 方法 設定即可:

C代碼  

  1. [btn1 setTitle:@"BTN1" forState:UIControlStateNormal];  

你也可以為按鈕的某一狀态設定為圖。用 setImage 即可:

C代碼  

  1. [btn2 setImage:[UIImage imageNamed:@"pic"] forState:UIControlStateNormal];  

此外,你還可以為每種按鈕狀态設定标題的顔色和陰影,以及按鈕的背景。方法 setTitleColor 和 setTitleShadowColor 都需要一個UIColor對象做參數:

C代碼  

  1. [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//設定标題顔色  
  2.     [btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//陰影  
  3.     [btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景圖像  

上面幾個方法都提到 共同的參數 forState . 這個參數決定了标題、圖像或其他屬性将在何種狀态下顯現。你可以程式設計令按鈕在那個狀态變化

C代碼  

  1. enum {  
  2.     UIControlStateNormal       = 0,  //常态                       
  3.     UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set 高亮  
  4.     UIControlStateDisabled     = 1 << 1,  //禁用  
  5.     UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below) 選中  
  6.     UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use 當應用程式标志使用時  
  7.     UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use  為内部架構預留的  
  8. };  
  9. typedef NSUInteger UIControlState;  

你隻要掌握前四種狀态就好了。

當按鈕高亮或者禁用,UIButton 類可以調整自己的外觀,下面幾個屬性可以讓你按照需要對按鈕的外觀進行微調:

adjustsImageWhenHighlighted

預設情況下,在按鈕被禁用時,圖像會被畫的顔色深一些。要禁用此功能,請将這個屬性設定為NO:

C代碼  

  1. btn1.adjustsImageWhenHighlighted = NO;  

adjustsImageWhenDisabled

預設情況下,按鈕在被禁用時,圖像會被畫的顔色淡一些。要禁用此功能,請将這個屬性設定為NO:

C代碼  

  1. btn1.adjustsImageWhenDisabled = NO;  

showsTouchWhenHighlighted

這個屬性設定為YES,可令按鈕在按下時發光。這可以用于資訊按鈕或者有些重要的按鈕:

C代碼  

  1. btn1.showsTouchWhenHighlighted = YES;  

三、顯示控件

 顯示控件一如繼往的簡單:

C代碼  

  1. [self.view addSubview:btn1];  
  2.     [self.view addSubview:btn2];  

四、重寫繪制行為

你可以通過子類化按鈕來定制屬于你自己的按鈕類。在子類化的時候你可以重載下面這些方法,這些方法傳回CGRect結構,指明了按鈕每一組成部分的邊界。

注意:不要直接調用這些方法, 這些方法是你寫給系統調用的。

C代碼  

  1. backgroundRectForBounds   //指定背景邊界  

C代碼  

  1. contentRectForBounds // 指定内容邊界  

C代碼  

  1. titleRectForContentRect    // 指定文字标題邊界  

C代碼  

  1. imageRectForContentRect     //指定按鈕圖像邊界  

例:

C代碼  

  1. - (CGRect)imageRectForContentRect:(CGRect)bounds{  
  2.      return CGRectMake(0.0, 0.0, 44, 44);  
  3.  }  

五、添加動作

按鈕是用來幹嘛的?用來激發某個動作或事件的。那我們我們要為他添加一個動作,與 UIControl 裡講的一樣:

C代碼  

  1. -(void)btnPressed:(id)sender{  
  2.     UIButton* btn = (UIButton*)sender;  
  3.     //開始寫你自己的動作  
  4. }  
  5.  [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];