標籤:
UIToolBar控制項:是經常使用的一個工具條控制項,雖然在上面可以添加子控制項,但是toolbar中只能添加UIBarButtonItem類型的子控制項,其他子控制項會被封裝成這種類型的,例如UIButton。通過工具列可以用來對視圖View中內容進行操作。
原理:
可以在toolBar上添加任何子控制項。其實它的原理是把你要添加的子控制項先加到toolbarItems數組裡面,最後再把toolbarItems數組一次性放到toolbar工具列裡面。
雖然可以在toolbar中添加其他任何的視圖控制項如UILabel、UITextField、UIImageView等等,但是在xib/storyboard圖形介面設計時,不能它們直接放置到UIToolBar中。若強行將它們拖曳到UIToolBar,會使它們放置在上層容器中,而不是UIToolBar中。所以前提是先必須添加一個視圖UIView控制項到toolbar中,它會被封裝成UIBarButtonItem類型,然後再在UIView中添加需要的子視圖控制項。
舉例如下:將UILabel加入到toolbar工具列中,步驟如下:
1. 將UIView拖曳到UIToolBar(UIToolBar中自動增加了一個UIBarButtonItem,其中便是剛才插入的UIView);
2. 將UILabel(或其他控制項)拖曳到剛才的UIView中;
3. 將剛才的UIView的Background設為某種顏色(如藍色);
4. 將剛才的UIView的Background設為Default。
對toolbar進行初始化:
-initWithTitle(添加button用這個)
-initWithImage
-initWithBarButtonSystemItem(添加系統自訂的button,形狀跟大小都已經固定了)下面連結裡面有按鈕圖片樣式
-initWithCustomView(添加除了button以外的View)
一、採用系統預設.xib檔案中的UIToolBar,製作的工具列(刪除和添加圖片)
(1)預設的View視圖布局
代碼如下:需要在代碼中為添加的控制項人為設定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)自訂的View視圖布局,添加UIImage、UILabel控制項
代碼如下:不需要在代碼中再去設定添加控制項的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
iOS:UIToolBar控制項的使用