iOS UIButton 常用設定

來源:互聯網
上載者:User

標籤:style   io   color   ar   os   使用   sp   for   on   

類方法buttonWithType

UIButton *btn= [UIButton buttonWithType:UIButtonTypeRoundedRect];

風格有如下

typedef enum {      UIButtonTypeCustom = 0,//自訂,無風格    UIButtonTypeRoundedRect,//白色圓角矩形,類似喜好設定表格單元或者地址簿卡片    UIButtonTypeDetailDisclosure,//藍色的披露按鈕,可放在任何文字旁    UIButtonTypeInfoLight,//微件(widget)使用的小圓圈資訊按鈕,可以放在任何文字旁    UIButtonTypeInfoDark,//白色背景下使用的深色圓圈資訊按鈕    UIButtonTypeContactAdd,//藍色加號(+)按鈕,可以放在任何文字旁} UIButtonType;

設定屬性

Frame屬性

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

CGRect btnFrame = CGRectMake(10.0, 10.0, 60.0, 44.0);

btn.frame =btnFrame;

屬性

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

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

你也可以為按鈕的某一狀態設定為圖。用Setimage即可

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

此外,你還可以為每種按鈕狀態設定標題的顏色和陰影,以及按鈕的背景。

方法 setTitleColor 和 setTitleShadowColor 都需要一個UIColor對象做參數

[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//設定標題顏色  [btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//陰影  [btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景映像
上面幾個方法都提到 共同的參數 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結構,指明了按鈕每一組成部分的邊界。

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

<pre name="code" class="objc">backgroundRectForBounds//指定背景邊界  contentRectForBounds//指定內容邊界  titleRectForContentRect//指定文字標題邊界  imageRectForContentRect//指定按鈕映像邊界  //例:- (CGRect)imageRectForContentRect:(CGRect)bounds{  return CGRectMake(0.0, 0.0, 44, 44);}



添加動作

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

[btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];-(void)btnPressed:(id)sender{UIButton* btn = (UIButton*)sender;}

iOS UIButton 常用設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.