Use of the Objective-c:uitoolbar control

Source: Internet
Author: User

Uitoolbar control: is a frequently used toolbar control, in which you can add many other controls, UILabel, UIButton, UIImage, Uibarbuttonitem, Uitextfield, and so on 、、。 A layout that can be used to customize the controls in view views. After the layout of the view is complete, it can be used directly, without the code to control the coordinates and size of the control, convenient and accurate.

First, using the system default. xib files in the Uitoolbar made add and remove strips (delete and add pictures)

(1) Add the Uitoolbar of the system setting with two labels label

  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 #defineContace_view_height 503 @interfaceViewcontroller ()4@property (Weak, nonatomic) Iboutlet Uitoolbar *ToolBar;5@property (Weak, nonatomic) Iboutlet Uibarbuttonitem *Barbuttonitemdelete;6 7 @end8 9 @implementationViewcontrollerTen-(Ibaction) Addcontact: (Uibarbuttonitem *) Sender One { A      -     //make the Delete button valid - [Self.barbuttonitemdelete Setenabled:yes]; the      -     //there are already 3 controls in Subview -Nsinteger count = Self.view.subviews.count-3; -CGRect Lastframe =Self.toolBar.frame; +      -UIView *contactview =[[UIView alloc]init]; +CGFloat gapy =5; ACGFloat x =0; atCGFloat y = lastframe.origin.y + lastFrame.size.height + (contace_view_height+gapy) *count; -CGFloat w =Self.view.frame.size.width; -      -Contactview.frame =CGRectMake (x,y,w,contace_view_height); -      -      in     //Add Avatar -UIImage *image = [UIImage imagenamed:[nsstring stringWithFormat:@"%d.png", Arc4random_uniform (9)]]; toUiimageview *face =[[Uiimageview alloc]initwithimage:image]; +Face.frame = CGRectMake (Ten,0, -, -); - [Contactview addsubview:face]; the      *     //Add Name $UILabel *labelname =[[UILabel alloc]init];Panax NotoginsenglabelName.Text = [NSString stringWithFormat:@"name%d", Arc4random_uniform (Ten)]; -Labelname.frame = CGRectMake (Ten+image.size.width+ -,0, -, -); the [Contactview addsubview:labelname]; +      A      theContactview.backgroundcolor =[Uicolor Lightgraycolor]; +      - [Self.view Addsubview:contactview]; $ } $-(Ibaction) DeleteContact: (Uibarbuttonitem *) Sender - { -     //Delete a view theUIView *lastview =[Self.view.subviews Lastobject]; - [Lastview Removefromsuperview];Wuyi      the     //If the Contactview is not available, the Set Delete button is invalid -     if(Self.view.subviews.count = =3) Wu     { - [Self.barbuttonitemdelete Setenabled:no]; About     } $ } -  -- (void) Viewdidload { - [Super Viewdidload]; A     //Delete is set to invalid at start + [Self.barbuttonitemdelete Setenabled:no]; the } -  $- (void) didreceivememorywarning { the [Super didreceivememorywarning]; the     //Dispose of any resources the can be recreated. the}

Second, using the pre-custom layout of the. xib file in the Uitoolbar made additions and deletions (delete and add pictures)

(2) Custom uitoolbar after adding 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.

1Import"ViewController.h"2 #defineContace_view_height 503 @interfaceViewcontroller ()4@property (Weak, nonatomic) Iboutlet Uitoolbar *ToolBar;5@property (Weak, nonatomic) Iboutlet Uibarbuttonitem *Barbuttonitemdelete;6 7 @end8 9 @implementationViewcontrollerTen-(Ibaction) Addcontact: (Uibarbuttonitem *) Sender One { A      -     //make the Delete button valid - [Self.barbuttonitemdelete Setenabled:yes]; the      -     //there are already 3 controls in Subview -Nsinteger count = Self.view.subviews.count-3; -CGRect Lastframe =Self.toolBar.frame; +      -     //load the Xib file +Nsarray *views = [[NSBundle mainbundle]loadnibnamed:@"Contactview"Owner:nil Options:nil]; A      at     //Add Contactview -UIView *contactview =[views Lastobject]; -  -      -CGFloat gapy =5; -CGFloat x =0; inCGFloat y = lastframe.origin.y + lastFrame.size.height + (contace_view_height+gapy) *count; -CGFloat w =Self.view.frame.size.width; toContactview.frame =CGRectMake (x,y,w,contace_view_height); +      -      the     //Add Avatar *Uiimageview *face = (Uiimageview *) [Contactview Viewwithtag:1]; $UIImage *image = [UIImage imagenamed:[nsstring stringWithFormat:@"%d.png", Arc4random_uniform (9)]];Panax Notoginseng [face setimage:image]; -  the  +      A     //Add Name theUILabel *labelname = (UILabel *) [Contactview Viewwithtag:2]; +labelName.Text = [NSString stringWithFormat:@"name%d", Arc4random_uniform (Ten)]; -      $ [Self.view Addsubview:contactview]; $ } -  --(Ibaction) DeleteContact: (Uibarbuttonitem *) Sender the { -     //Delete a viewWuyiUIView *lastview =[Self.view.subviews Lastobject]; the [Lastview Removefromsuperview]; -      Wu     //If the Contactview is not available, the Set Delete button is invalid -     if(Self.view.subviews.count = =3) About     { $ [Self.barbuttonitemdelete Setenabled:no]; -     } - } -  A- (void) Viewdidload { + [Super Viewdidload]; the     //Delete is set to invalid at start - [Self.barbuttonitemdelete Setenabled:no]; $ } the  the- (void) didreceivememorywarning { the [Super didreceivememorywarning]; the     //Dispose of any resources the can be recreated. - } in  the @end

Use of the Objective-c:uitoolbar control

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.