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 ViewController 10 - (IBAction)addContact:(UIBarButtonItem *)sender 11 { 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 *)sender 47 { 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 ViewController 10 - (IBAction)addContact:(UIBarButtonItem *)sender 11 { 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 //添加contactView 24 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 *)sender 49 { 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