IOS開發之XCode學習003:UIButton基礎

來源:互聯網
上載者:User

標籤:loading   ima   添加   風格   檔案   ios   view   ted   stat   

此文學習來源為:http://study.163.com/course/introduction/1002858003.htm

 

此工程檔案實現功能:

 1、UIButton的控制項基本概念

2、UIButton的建立方法

3、UIButton的類型

4、可顯示圖片的UIButton

可將準備好的圖片直接拖到工程名字UIButton下

 

 

===========================ViewController.m指令碼==============================

//建立普通按鈕函數

-(void)CreateUIRectButton

{

    //建立一個btn對象,根據類型來建立btn

    //圓角類型

    //通過類方法來建立buttonWithType:類名 + 方法名

    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];//自己管理記憶體

    

    //設定button按鈕的位置

    btn.frame = CGRectMake(100, 100, 100, 40);

    

    //設定按鈕的文字內容

    //@parameter

    //P1:字串類型,顯示到按鈕上的文字

    //P2:設定文字顯示的狀態類型:UIControlStateNormal,正常狀態

    [btn setTitle:@"按鈕01" forState:UIControlStateNormal];

    

    //P1:顯示的文字

    //P2:顯示文字的狀態:UIControlStateHighlighted,按下狀態

    [btn setTitle:@"按鈕按下" forState:UIControlStateHighlighted];

    

    //灰色背景顏色

    btn.backgroundColor = [UIColor grayColor];

    

    //設定文字顯示的顏色

    //P1:顏色

    //P2:狀態

    //[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    

    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];

    

    //設定按鈕的風格顏色

    [btn setTintColor:[UIColor whiteColor]];

    

    //titleLabel:UILabel控制項

    btn.titleLabel.font = [UIFont systemFontOfSize:24];

    

    //添加到視圖中並顯示

    [self.view addSubview:btn];

}

 

-(void)createImageBtn

{

    //建立一個自訂類型的btn

    UIButton* btnImage = [UIButton buttonWithType:UIButtonTypeCustom];

    

    btnImage.frame = CGRectMake(100, 200, 100, 100);

    

    UIImage* icon01 = [UIImage imageNamed:@"btn01.jpg"];

    

    UIImage* icon02 = [UIImage imageNamed:@"btn02.jpg"];

    

    //設定按鈕圖片

    //P1:顯示的圖片對象

    //P2:控制項的狀態

    [btnImage setImage:icon01 forState:UIControlStateNormal];

    

    [btnImage setImage:icon02 forState:UIControlStateHighlighted];

    

    [self.view addSubview:btnImage];

}

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [self CreateUIRectButton];

    [self createImageBtn];

}

學習總結:

  • 重點:UIButton的屬性用法
  • 痛點:UIButton顯示圖片

源碼連結地址:https://pan.baidu.com/s/1yrOLXZZeu9MiOWtMq5-EGA  密碼:7t1l

IOS開發之XCode學習003: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.