Use macros to define text entries, image entries, system entries, and custom view entries, which provide an automatic release uibarbuttonitem that can be placed into the uitoolbar.
#define Cookbook_purple_color [Uicolor colorwithred:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
#define Barbutton (TITLE, SELECTOR) [[[Uibarbuttonitem alloc] Initwithtitle:title style:uibarbuttonitemstyleplain Targ Et:self Action:selector] autorelease]
#define Imgbarbutton (IMAGE, SELECTOR) [[[Uibarbuttonitem alloc] Initwithimage:image style:uibarbuttonitemstyleplain Target:self Action:selector] autorelease]
#define Sysbarbutton (ITEM, SELECTOR) [[[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:item target:self action: SELECTOR] Autorelease]
#define Custombarbutton (VIEW) [[Uibarbuttonitem alloc] Initwithcustomview:view] autorelease]
Create a toolbar
Uitoolbar *TB = [[Uitoolbar alloc] Initwithframe:cgrectmake (0.0f, 0.0f, 320.0f, 44.0f)];
Tb.center = Cgpointmake (160.0f, 200.0f);
Nsmutablearray *tbitems = [Nsmutablearray array];
[Tbitems Addobject:barbutton (@ "Title", @selector (Action))];
[Tbitems Addobject:sysbarbutton (Uibarbuttonsystemitemadd, @selector (action))];
[Tbitems Addobject:imgbarbutton ([uiimage imagenamed:@ "Tbumbrella.png"], @selector (action))];
[Tbitems Addobject:custombarbutton ([[[[[Uiswitch alloc] init] autorelease])];
[Tbitems Addobject:sysbarbutton (Uibarbuttonsystemitemflexiblespace, nil)];
[Tbitems Addobject:imgbarbutton ([uiimage imagenamed:@ "Tbpuzzle.png"], @selector (action))];
ADD Fixed pixel width
Uibarbuttonitem *bbi = [[[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemfixedspace target: Nil Action:nil] autorelease];
Bbi.width = 20.0f;
[Tbitems Addobject:bbi];
Tb.items = Tbitems;
[Self.view ADDSUBVIEW:TB];
[TB release];
Toolbar tip:
Working with toolbars is a useful way to use some tips.
1, fixed space can have width ----in all Uibarbuttonitem, only uibarbuutonsystemitemfixedspace entries can be assigned a width, so create a space entry, set its width, It is then added to the entry column.
2. use a flexible space for left or right alignment---starting with a uibarbuttonsystemitemflexiblespace in the entry list causes all remaining entries to align to the right. Add one at the end, then align left. With two uibarbuttonsystemitemflexiblespace, one added at the beginning and one at the end, the remaining entries are centered.
3, consider missing items-------------Hide Bar button entries according to the context, do not use only flexible space allocation to remove entries. Instead, replace the entry with a fixed-width space that matches the original size of the entry. Doing so saves the layout before and after the entry disappears and keeps all other icons in place.