Objective-C:UIToolBar控制項的使用

來源:互聯網
上載者:User

標籤:

UIToolBar控制項:是經常使用的一個工具條控制項,在它裡面可以添加很多其他控制項,UILabel、UIButton、UIImage、UIBarButtonItem、UITextField等等、、。可以用來對視圖view中控制項進行自訂的布局。視圖布局完成後,可以直接拿來用,不用再用代碼去控制控制項的座標和大小,方便而且準確。

一、採用系統預設.xib檔案中的UIToolBar製作的增刪條(刪除和添加圖片)

  (1)添加有兩個標籤Label的系統設定的UIToolBar

     

  代碼如下:需要在代碼中為添加的控制項人為設定frame具體座標x,y、大小width,height

 1 #import "ViewController.h" 2 #define CONTACE_VIEW_HEIGHT 50 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIToolbar *toolBar; 5 @property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete; 6  7 @end 8  9 @implementation ViewController10 - (IBAction)addContact:(UIBarButtonItem *)sender11 {12     13     //讓刪除按鈕有效14     [self.barButtonitemDelete setEnabled:YES];15     16     //在subView中已經有了3個控制項17     NSInteger count = self.view.subviews.count - 3;18     CGRect lastFrame = self.toolBar.frame;19     20     UIView *contactView = [[UIView alloc]init];21     CGFloat gapY = 5;22     CGFloat x = 0;23     CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;24     CGFloat w = self.view.frame.size.width;25     26     contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT);27     28     29     //添加頭像30     UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]];31     UIImageView *face = [[UIImageView alloc]initWithImage:image];32     face.frame = CGRectMake(10, 0,50,50);33     [contactView addSubview:face];34     35     //添加姓名36     UILabel *labelName = [[UILabel alloc]init];37     labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform(10)];38     labelName.frame = CGRectMake(10+image.size.width+50, 0, 100, 50);39     [contactView addSubview:labelName];40     41     42     contactView.backgroundColor = [UIColor lightGrayColor];43     44     [self.view addSubview:contactView];45 }46 - (IBAction)deleteContact:(UIBarButtonItem *)sender47 {48     //刪除視圖49     UIView *lastView = [self.view.subviews lastObject];50     [lastView removeFromSuperview];51     52     //如果沒有了contactView,設定刪除按鈕無效53     if(self.view.subviews.count == 3)54     {55         [self.barButtonitemDelete setEnabled:NO];56     }57 }58 59 - (void)viewDidLoad {60     [super viewDidLoad];61     //開始時刪除設定為無效62     [self.barButtonitemDelete setEnabled:NO];63 }64 65 - (void)didReceiveMemoryWarning {66     [super didReceiveMemoryWarning];67     // Dispose of any resources that can be recreated.68 }

 

  二、採用提前自訂布局的.xib檔案中的UIToolBar製作的增刪條(刪除和添加圖片)

(2)添加UIImage、UILabel控制項後自訂的UIToolBar

    

    代碼如下:不需要在代碼中再去設定添加控制項的frame,在.xib檔案中已經布局好了。

 1 import "ViewController.h" 2 #define CONTACE_VIEW_HEIGHT 50 3 @interface ViewController () 4 @property (weak, nonatomic) IBOutlet UIToolbar *toolBar; 5 @property (weak, nonatomic) IBOutlet UIBarButtonItem *barButtonitemDelete; 6  7 @end 8  9 @implementation ViewController10 - (IBAction)addContact:(UIBarButtonItem *)sender11 {12     13     //讓刪除按鈕有效14     [self.barButtonitemDelete setEnabled:YES];15     16     //在subView中已經有了3個控制項17     NSInteger count = self.view.subviews.count - 3;18     CGRect lastFrame = self.toolBar.frame;19     20     //載入xib檔案21     NSArray *views = [[NSBundle mainBundle]loadNibNamed:@"contactView" owner:nil options:nil];22     23     //添加contactView24     UIView *contactView = [views lastObject];25 26     27     CGFloat gapY = 5;28     CGFloat x = 0;29     CGFloat y = lastFrame.origin.y + lastFrame.size.height + (CONTACE_VIEW_HEIGHT+gapY) * count;30     CGFloat w = self.view.frame.size.width;31     contactView.frame = CGRectMake(x,y,w,CONTACE_VIEW_HEIGHT);32     33     34     //添加頭像35     UIImageView *face = (UIImageView *)[contactView viewWithTag:1];36     UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png",arc4random_uniform(9)]];37     [face setImage:image];38 39 40     41     //添加姓名42     UILabel *labelName = (UILabel *)[contactView viewWithTag:2];43     labelName.text = [NSString stringWithFormat:@"name%d",arc4random_uniform(10)];44     45     [self.view addSubview:contactView];46 }47 48 - (IBAction)deleteContact:(UIBarButtonItem *)sender49 {50     //刪除視圖51     UIView *lastView = [self.view.subviews lastObject];52     [lastView removeFromSuperview];53     54     //如果沒有了contactView,設定刪除按鈕無效55     if(self.view.subviews.count == 3)56     {57         [self.barButtonitemDelete setEnabled:NO];58     }59 }60 61 - (void)viewDidLoad {62     [super viewDidLoad];63     //開始時刪除設定為無效64     [self.barButtonitemDelete setEnabled:NO];65 }66 67 - (void)didReceiveMemoryWarning {68     [super didReceiveMemoryWarning];69     // Dispose of any resources that can be recreated.70 }71 72 @end

 

Objective-C:UIToolBar控制項的使用

聯繫我們

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