(轉載請保留此文字:本文來源:[iphone開發私房菜_2_] 為UIToolBar工具條添加各種各樣的UIBbarButtonItem http://blog.csdn.net/ipromiseu/archive/2010/12/16/6080689.aspx] write by Gray.Luo guohui.great@gmail.com)
1.首先,我們看一下UIBbarButtonItem有哪些初始化方法,這也可以看出,它可以被定義為什么東東,然后加到UIToolBar上面去。
根據SDK的文檔,我們可以發現UIBbarButtonItem有如下幾種初始化的方法:
-initWithTitle
-initWithImage
-initWithBarButtonSystemItem
-initWithCustomView
第4種方法就是我們添加各種作料的接口,所以今天的主角其它也是它。
2.在UIToolBar上面添加Title
- UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:
- CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
- NSMutableArray *myToolBarItems = [NSMutableArray array];
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithTitle:@"myTile"
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(action)] autorelease]];
- [myToolBar setItems:myToolBarItems animated:YES];
- [myToolBar release];
- [myToolBarItems];
setItems傳入值或者說items是一個對象數組。
3.在UIToolBar上面添加image
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithImage:[UIImage imageNamed:@"myImage.png"]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(action)]];
4.在UIToolBar上面添加SystemItem
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
- target:self
- action:@selector(action)] autorelease]];
Note:
initWithBarButtonSystemItem初始化:
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action
Defines system defaults for commonly used items.
- typedef enum {
- UIBarButtonSystemItemDone,
- UIBarButtonSystemItemCancel,
- UIBarButtonSystemItemEdit,
- UIBarButtonSystemItemSave,
- UIBarButtonSystemItemAdd,
- UIBarButtonSystemItemFlexibleSpace,
- UIBarButtonSystemItemFixedSpace,
- UIBarButtonSystemItemCompose,
- UIBarButtonSystemItemReply,
- UIBarButtonSystemItemAction,
- UIBarButtonSystemItemOrganize,
- UIBarButtonSystemItemBookmarks,
- UIBarButtonSystemItemSearch,
- UIBarButtonSystemItemRefresh,
- UIBarButtonSystemItemStop,
- UIBarButtonSystemItemCamera,
- UIBarButtonSystemItemTrash,
- UIBarButtonSystemItemPlay,
- UIBarButtonSystemItemPause,
- UIBarButtonSystemItemRewind,
- UIBarButtonSystemItemFastForward,
- UIBarButtonSystemItemUndo, // iPhoneOS 3.0
- UIBarButtonSystemItemRedo, // iPhoneOS 3.0
- } UIBarButtonSystemItem;
5.在UIToolBar上面添加其它各種控件,最自由意義,最有意思的,我把它放在最后來講。我們使用initWithCustomView來完成,
這里需要看一下initWithCustomView的定義:
- (id)initWithCustomView:(UIView *)customView
可以看出,它的參數是一個VIEW,所以我們給它的配料要正確哦才行哦,否則,你就等著時間DIDADIDA的流失吧.
A>加一個開關switch:
- [myToolBarItems addObject:[[[UIBarButtonItem alloc]
- initWithCustomView:[[[UISwitch alloc] init] autorelease]]
- autorelease]];
B>加一個按鈕UIBarButtonItem
- UIBarButtonItem *myButton = [[[UIBarButtonItem alloc]
- initWithTitle:@"myButton"
- style:UIBarButtonItemStyleBordered
- target:self
- action:@selector(action)]autorelease];
- get1Button.width = 50;
- [myToolBarItems addObject:myButton];
C>加一個文本Label
- UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(40.0f, 20.0f, 45.0f, 10.0f)];
- myLabel.font=[UIFont systemFontOfSize:10];
- //myLabel.backgroundColor = [UIColor clearColor];
- //myLabel.textAlignment=UITextAlignmentCenter;
- UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myLabel];
- [myToolBarItems addObject: myButtonItem];
- [mylabel release];
- [myButtonItem release];
D>加一個進度條UIProgressView
- UIProgressView *myProgress = [[UIProgressView alloc] initWithFrame:CGRectMake(65.0f, 20.0f, 90.0f, 10.0f)];
- UIBarButtonItem *myButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myProgress];
- [myToolBarItems addObject: myButtonItem];
- [myProgress release];
- [myButtonItem release];
可以加使用initWithCustomView制作各種button,這里就不在這里一個一個在加了。我想你應該也已經掌握了如何添加各種buttonItem的方法了。