IOS基礎學習-2: UIButton,ios基礎-2uibutton

來源:互聯網
上載者:User

IOS基礎學習-2: UIButton,ios基礎-2uibutton

    

UIButton是一個標準的UIControl控制項,UIKit提供了一組控制項:UISwitch開關、UIButton按鈕、UISegmentedControl分段控制項、UISlider滑塊、UITextField文字欄位控制項、UIPageControl分頁控制項。這些控制項的基類均是UIControl,而UIControl派生自UIView類,所以每個控制項都有很多視圖的特性,包括附著於其他視圖的能力。所有控制項都擁有一套共同的屬性和方法。

具體視圖關係如下

1. 建立按鈕 1.1 initWithFrame
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];
1.2 類方法buttonWithType

通常採用自訂方法,這樣的方式比較靈活

[UIButton buttonWithType:UIButtonTypeCustom];

和UIButtonTypeCustom等同的還有以下方式:

typedef enum {      UIButtonTypeCustom = 0,               // 自訂,常用方式     UIButtonTypeRoundedRect,              //白色圓角矩形,類似喜好設定表格單元或者地址簿卡片      UIButtonTypeDetailDisclosure,    //藍色的披露按鈕,可放在任何文字旁      UIButtonTypeInfoLight,        //小圓圈資訊按鈕,可以放在任何文字旁      UIButtonTypeInfoDark,        //白色背景下使用的深色圓圈資訊按鈕      UIButtonTypeContactAdd,        //藍色加號(+)按鈕,可以放在任何文字旁  } UIButtonType;
2. 設定按鈕屬性2.1 Frame屬性
btn.frame = CGRectMake(10.0, 10.0, 60.0, 44.0);  //x, y , weight, height;

或者:

[btn setFrame:CGRectMake(20,20,50,50)];
2.2 設定標題
[btn1 setTitle:@"點擊" forState:UIControlStateNormal];  //設定標題[btn1 setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; //設定標題顏色[btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//標題陰影顏色
2.3設定背景圖片
 
[btn1 setImage:[UIImageimageNamed:@"btn1Img"] forState:UIControlStateNormal];   //按鈕圖片[btn1 setBackgroundImage:[UIImageimageNamed:@"btn1BgImg"] forState:UIControlStateNormal];  //背景圖片
2.4 設定按鈕圖片內部間距
UIEdgeInsets insets;     // 設定按鈕內部圖片間距insets.top = insets.bottom = insets.right = insets.left = 10;btn.contentEdgeInsets = insets;    //內容間距    bt.titleEdgeInsets = insets;         // 標題間距

 

3. 按鈕狀態3.1 常用狀態

forState後,可以選擇以下狀態,常用的包括:常態,高亮,禁用,選中;

enum {      UIControlStateNormal       = 0,          //常態                           UIControlStateHighlighted  = 1 << 0,                      // 高亮      UIControlStateDisabled     = 1 << 1,          //禁用      UIControlStateSelected     = 1 << 2,                      //選中      UIControlStateApplication  = 0x00FF0000,          //當應用程式標誌使用時      UIControlStateReserved     = 0xFF000000          //為內部架構預留的  };  typedef NSUInteger UIControlState;
3.2高亮或者禁用時,微調屬性

高亮時,映像顏色會加深,如果要關閉此屬性,請設定adjustsImageWhenHighlighted 為NO

btn1.adjustsImageWhenHighlighted = NO;

禁用時,映像顏色會變淺,如果要關閉此屬性,請設定adjustsImageWhenDisabled 為NO

btn1.adjustsImageWhenDisabled = NO;

選中時,可以設定按鈕發光,請設定showsTouchWhenHighlighted 為YES

btn1.showsTouchWhenHighlighted = YES;
4. 添加動作
[btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];   //為按鈕的行為添加屬性-(void)btnPressed:(id)sender{              //處理按鈕按下事件    UIButton* btn = (UIButton*)sender;      if(btn ==  btn 1){    NSLog(@"btn1 pressed");    }}
5. 執行個體

具體見下面的例子。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.