Uitoolbar Controls: A Toolbar control that is used frequently, although you can add child controls on it, but only child controls of type Uibarbuttonitem are added to toolbar, and other child controls are wrapped into this type, such as UIButton. Toolbars allow you to manipulate the contents of a view.
Principle:
You can add any child controls on the toolbar. In fact, it is the principle of adding the child control you want to add to the ToolbarItems array , and finally put the ToolbarItems array into the toolbar toolbar once.
Although you can add any other view controls such as Uilabel, Uitextfield, Uiimageview, and so on in toolbar, you cannot place them directly in Xib/storyboard when you design the Uitoolbar graphical interface. If you drag them to Uitoolbar, they will be placed in the upper container instead of Uitoolbar. So the premise is that you first have to add a view UIView control to toolbar, which is wrapped into a uibarbuttonitem type, and then add the desired child view control in UIView.
For example, add Uilabel to the toolbar toolbar, as follows:
1. Drag the UIView to Uitoolbar (Uitoolbar automatically adds a uibarbuttonitem, which is the uiview just inserted);
2. Drag the Uilabel (or other control) to the UIView that you just made;
3. Set the background of the uiview just to a certain color (e.g. blue);
4. Set the background of the UIView just as default.
Initialize the toolbar:
-initwithtitle (add button with this)
-initwithimage
-initwithbarbuttonsystemitem (Add system-customized button, shape and size are fixed) The following link has a button picture style
-initwithcustomview (add a view other than button)
First, using the system default. xib files in the Uitoolbar, made the toolbar (delete and add pictures)
(1) Default view view layout
The code is as follows: You need to manually set the frame specific coordinates x, y, size width,height for the added control in your code
1 #import "ViewController.h" 2 #define Contace_view_height 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//Let Delete button has effect [Self.barbuttonitemdelete setenabled:yes];15 16//already has 3 controls in Subview nsinteger count = SELF.VIEW.SUBV iews.count-3;18 cgrect lastframe = self.toolbar.frame;19 UIView *contactview = [[UIView alloc]init];21 CGFloat gapy = 5;22 cgfloat x = 0;23 CGFloat y = lastframe.origin.y + lastFrame.size.height + (Contace_view_heig ht+gapy) * count;24 cgfloat w = self.view.frame.size.width;25 contactview.frame = CGRectMake (x,y,w,CONTACE_ View_height); 27 28 29//Add avatar UIImage *image = [UIImage imagenamed:[nsstring stringwithformat:@ "%d.png", a Rc4random_uniform (9)]];31 UiimagevIew *face = [[Uiimageview alloc]initwithimage:image];32 face.frame = CGRectMake (0,50,50); [ContactView AddSu] BVIEW:FACE];34 35//Add name UILabel *labelname = [[UILabel alloc]init];37 labelName.Text = [NSString string withformat:@ "name%d", Arc4random_uniform (Ten)];38 labelname.frame = CGRectMake (10+image.size.width+50, 0, 100, 50); 39 [Contactview addsubview:labelname];40 contactview.backgroundcolor = [Uicolor lightgraycolor];43 44 [Self.view addsubview:contactview];45}46-(ibaction) DeleteContact: (Uibarbuttonitem *) sender47 {48//delete View 49 UIView *lastview = [Self.view.subviews lastobject];50 [Lastview removefromsuperview];51 52//If there is no Contactview, Set Delete button is invalid if (Self.view.subviews.count = = 3) [Self.barbuttonitemdelete setenabled:no];56}57}58 -(void) viewdidload {[Super viewdidload];61///start delete set to invalid [Self.barbuttonitemdelete setenabled:no];63 }64-(void) Didreceivememorywarning {$ [Super didreceivememorywarning];67//Dispose of any of the resources that can is recreated.68}
Second, using the pre-custom layout of the. xib file in the Uitoolbar, made the toolbar (delete and add pictures)
(2) Custom view layout, add UIImage, Uilabel controls
The code is as follows: You do not need to set the frame for the added control in your code, it is already laid out in the. xib file.
1 Import "ViewController.h" 2 #define Contace_view_height 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//Let Delete button has effect [Self.barbuttonitemdelete setenabled:yes];15 16//already has 3 controls in Subview nsinteger count = SELF.VIEW.SUBV iews.count-3;18 CGRect lastframe = self.toolbar.frame;19 20//load Xib file Nsarray *views = [[NSBundle main] bundle]loadnibnamed:@ "Contactview" Owner:nil options:nil];22 23//Add contactView24 UIView *contactview = [views lastobject];25 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 = CGRe Ctmake (x,y,w,contace_view_height); 32 33 34//Add avatar Uiimageview *face = (Uiimageview *) [Contactview viewwithtag:1];36 UIImage *image = [Uiima GE imagenamed:[nsstring stringwithformat:@ "%d.png", Arc4random_uniform (9)]];37 [face setimage:image];38 39 40 41 Add name UILabel *labelname = (UILabel *) [contactview viewwithtag:2];43 labelname.text = [NSString stringwithfor mat:@ "name%d", Arc4random_uniform (Ten)];44 [Self.view addsubview:contactview];46}47-(ibaction) deleteContact :(Uibarbuttonitem *) sender49 {50//delete view Wuyi UIView *lastview = [Self.view.subviews lastobject];52 [Lastview Remo VEFROMSUPERVIEW];53 54//If there is no Contactview, the Set Delete button is invalid if (Self.view.subviews.count = = 3) . barbuttonitemdelete setenabled:no];58}59}60-(void) viewdidload {[super viewdidload];63//start Delete set to invalid 6 4 [Self.barbuttonitemdelete setenabled:no];65}66-(void) didreceivememorywarning {Didreceivememorywa rning];69//Dispose of any resources the can be recreated.70}71 @end
Use of the Ios:uitoolbar control