IOS: Toolbar control Uitoolbar and toolbar button control Uibarbuttonitem use

Source: Internet
Author: User

One, Toolbar control:Uitoolbar:uiview

Introduction:The toolbar toolbar is a property of the views view, and you can add toolbar buttons to the toolbar button Item (either custom or system-brought Barbuttonsystemitem). The view controller can manipulate the contents of the image from the toolbar item. Precautions :there will be a Uitoolbar instance in the navigation bar controller, but it is hidden by default and needs to be opened by this method if it needs to be displayed:
It is important to note that, like Uinavigationbar, the navigation controller owns and has only one Uitoolbar instance, but the Uibarbuttonitem instance owned by Uitoolbar is managed by the view controller as follows:  Toolbar style:

typedef ns_enum (Nsinteger, Uibarstyle) {

Uibarstyledefault = 0, //default style, blue text

Uibarstyleblack = 1, //black background, brown text

Uibarstyleblackopaque = 1, //solid black background, white text

Uibarstyleblacktranslucent = 2, //transparent black background, white text

};

Properties:

@property (nonatomic) Uibarstyle Barstyle; //toolbar style, default to blue

@property (nonatomic,copy) Nsarray *items; //toolbar button unit ,uibarbuttonitem

@property (nonatomic,assign,getter=istranslucent) BOOL translucent //Transparent

@property (Nonatomic,retain) Uicolor *tintcolor; //Button color

@property (Nonatomic,retain) uicolor *bartintcolor; //Toolbar color

Method:※ Set the button unit in the toolbar

-(void) Setitems: (Nsarray *) Items animated: (BOOL) animated;

※ Setting the background image of the toolbar

-(void) SetBackgroundImage: (UIImage *) backgroundimage fortoolbarposition: (uibarposition) Toporbottom barMetrics: ( Uibarmetrics) Barmetrics;

※ Get the background image of the toolbar

-(UIImage *) Backgroundimagefortoolbarposition: (uibarposition) Toporbottom barmetrics: (uibarmetrics) BarMetrics ;  

※ Set the shadow image of the toolbar

-(void) Setshadowimage: (UIImage *) shadowImage fortoolbarposition: (uibarposition) Toporbottom;

※ Get shadow Image of toolbar

-(UIImage *) Shadowimagefortoolbarposition: (uibarposition) Toporbottom;

Second, toolbar button control: Uibarbuttonitem:uibaritem

Property:

@property (nonatomic) Uibarbuttonitemstyle style; //Style

@property (nonatomic) cgfloat width; //adjust pitch width

@property (nonatomic,copy) Nsset   *possibletitles; //title

@property (nonatomic,retain) UIView *customview; //Custom View

@property (nonatomic) SEL action; //Events

@property (nonatomic,assign) ID target; //target Agent

button Unit style Uibarbuttonitem:

typedef ns_enum (Nsinteger, Uibarbuttonitemstyle) {

Uibarbuttonitemstyleplain, //General style

uibarbuttonitemstylebordered, //style with borders

Uibarbuttonitemstyledone, //Blue style

};

System comes with the button:

typedef ns_enum (Nsinteger, Uibarbuttonsystemitem) {

Uibarbuttonsystemitemdone, //Confirm button

Uibarbuttonsystemitemcancel, //Cancel button

Uibarbuttonsystemitemedit, //Edit button

Uibarbuttonsystemitemsave, //Save button

Uibarbuttonsystemitemadd, //Add button

Uibarbuttonsystemitemflexiblespace,//Auto-adjust spacing button

Uibarbuttonsystemitemfixedspace, //Custom adjust spacing press button

Uibarbuttonsystemitemcompose,

Uibarbuttonsystemitemreply,

Uibarbuttonsystemitemaction,

Uibarbuttonsystemitemorganize,

Uibarbuttonsystemitembookmarks,

Uibarbuttonsystemitemsearch,

Uibarbuttonsystemitemrefresh,

Uibarbuttonsystemitemstop,

Uibarbuttonsystemitemcamera,

Uibarbuttonsystemitemtrash,

Uibarbuttonsystemitemplay,

Uibarbuttonsystemitempause,

Uibarbuttonsystemitemrewind,

Uibarbuttonsystemitemfastforward,

Uibarbuttonsystemitemundo,

Uibarbuttonsystemitemredo,

Uibarbuttonsystemitempagecurl,

};

Main methods:

※ Initialize with image

-(Instancetype) Initwithimage: (UIImage *) Image style: (Uibarbuttonitemstyle) style target: (ID) Target action: (SEL) Action

※ pictures (including vertical screen and horizontal screen show different pictures)

-(Instancetype) Initwithimage: (UIImage *) image landscapeimagephone: (UIImage *) Landscapeimagephone style: ( Uibarbuttonitemstyle) style target: (ID) Target action: (SEL) Action;

* Class with String.

-(Instancetype) Initwithtitle: (NSString *) Title style: (Uibarbuttonitemstyle) style target: (ID) Target action: (SEL) Action

* Initialize with System button

-(Instancetype) Initwithbarbuttonsystemitem: (Uibarbuttonsystemitem) Systemitem target: (ID) Target action: (SEL) Action

* Class with Custom image

-(Instancetype) Initwithcustomview: (UIView *) CustomView;

Examples are as follows:

1. Add the system's own button unit to the toolbar:

   //Create a Toolbal navigation bar instanceUitoolbar *toolbar = [[Uitoolbar alloc]initwithframe:cgrectmake (0,667- -,375, +)]; //Set Toolbar styleToolbar.barstyle =Uibarstyleblack; //Add the control to the view[Self.view Addsubview:toolbar]; //Create a button cell on a controlUibarbuttonitem *additem =[[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target:self Action:nil]; Uibarbuttonitem*edititem =[[Uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemedit target:self Action:nil]; Uibarbuttonitem*titleitem = [[Uibarbuttonitem alloc]initwithtitle:@"title"style:uibarbuttonitemstyledone target:self Action:nil]; //Create a flexible Adjustment button unit, set the intervalUibarbuttonitem *flexibleitem =[[Uibarbuttonitem Alloc]initwithbarbuttonsystemitem: (uibarbuttonsystemitemflexiblespace) target:self action:        NIL]; //Add a button cell to an arrayNsarray *items =@[additem,flexibleitem,edititem,flexibleitem,titleitem]; //Set the button cell on the navigation bar[Toolbar Setitems:items Animated:yes];

The demo results are as follows:

2. Add an image button on the toolbar

   //Create a Toolbal navigation bar instanceUitoolbar *toolbar = [[Uitoolbar alloc]initwithframe:cgrectmake (0, -,375, +)]; //Set Toolbar styleToolbar.barstyle =Uibarstyledefault; //Add the control to the view[Self.view Addsubview:toolbar]; //Create a button cell on a controlUibarbuttonitem *imageitem1 = [[Uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@"1.png"] Style:uibarbuttonitemstyleplain target:self Action:nil]; Uibarbuttonitem*IMAGEITEM2 = [[Uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@"2.png"] Style:uibarbuttonitemstyleplain target:self Action:nil]; Uibarbuttonitem*IMAGEITEM3 = [[Uibarbuttonitem alloc]initwithimage:[uiimage imagenamed:@"3.png"] Style:uibarbuttonitemstyleplain target:self Action:nil]; //Create a flexible Adjustment button unit, set the intervalUibarbuttonitem *flexibleitem =[[Uibarbuttonitem Alloc]initwithbarbuttonsystemitem: (uibarbuttonsystemitemflexiblespace) target:self action:        NIL]; //Add a button cell to an arrayNsarray *items =@[imageitem1,flexibleitem,imageitem2,flexibleitem,imageitem3]; //Set the button cell on the navigation bar[Toolbar Setitems:items Animated:yes];

The demo results are as follows:

IOS: Toolbar control Uitoolbar and toolbar button control Uibarbuttonitem use

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.