IOS基礎控制項--Button

來源:互聯網
上載者:User

標籤:

一、拖拽控制項法

拖拽添加一個Button Type 為custom,分別設定State config分別為Default, highlighted, selected.並分別選擇Background為image全稱。

 1 #import <UIKit/UIKit.h> 2  3 @interface ViewController : UIViewController 4  5 @property (nonatomic, strong) IBOutlet UIButton *btn; 6  7 @end 8  9 .m檔案中10 - (IBAction)btnSelected:(id)sender11 {12     UIButton *btn = (UIButton *)sender;13     btn.selected = YES;14 }15 16 完成操作

二、手寫代碼法

 1 //宏定義檔案 2 #ifndef Globaldefine_h 3 #define Globaldefine_h 4  5 #define SCREENWIDTH [[UIScreen mainScreen] bounds].size.width 6 #define SCREENHEIGHT [[UIScreen mainScreen] bounds].size.height 7  8 #endif /* Globaldefine_h */ 9 10 11 //視圖控制器.h檔案12 #import <UIKit/UIKit.h>13 14 @interface ViewController : UIViewController15 16 @property (nonatomic, strong) IBOutlet UIButton *btn;17 18 @end19 20 #import "ViewController.h"21 #import "Globaldefine.h"22 23 @interface ViewController ()24 25 @end26 27 @implementation ViewController28 29 - (void)viewDidLoad30 {31     [super viewDidLoad];32     _btn = [UIButton buttonWithType:UIButtonTypeCustom];//按鈕狀態33     _btn.frame = CGRectMake(SCREENWIDTH / 2 - 30, SCREENHEIGHT / 2 - 30, 60, 60);//設定按鈕位置和大小34     [_btn setBackgroundImage:[UIImage imageNamed:@"xmh.png"] forState:UIControlStateNormal];35      [_btn setBackgroundImage:[UIImage imageNamed:@"wdh.png"] forState:UIControlStateHighlighted];36      [_btn setBackgroundImage:[UIImage imageNamed:@"syh.png"] forState:UIControlStateSelected];37     [self.view addSubview:_btn];38     39     40     [_btn addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];//@selector(btnSelected:)冒號就是把_btn傳過去41 }42 - (void)btnSelected:(UIButton *)btn43 {44     if (btn.selected)45     {46         btn.selected = NO;47     }48     else49     {50         btn.selected = YES;51     }52 }53 54 - (void)didReceiveMemoryWarning {55     [super didReceiveMemoryWarning];56     // Dispose of any resources that can be recreated.57 }58 59 @end

 

IOS基礎控制項--Button

聯繫我們

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