为UIToolBar工具条添加各种各样的UIBbarButtonItem

本文介绍如何使用四种不同方法初始化UIBarButtonItem以实现按钮的各种样式和功能,包括文本、图片、系统图标及自定义视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据SDK的文档,我们可以发现UIBbarButtonItem有如下几种初始化的方法:

-initWithTitle

-initWithImage

-initWithBarButtonSystemItem

-initWithCustomView

1:在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];
2:在UIToolBar上面添加image
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"myImage.png"]
                                                    style:UIBarButtonItemStylePlain
            target:self
    action:@selector(action)]];
3:在UIToolBar上面添加SystemItem
[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay
    target:self 
    action:@selector(action)] autorelease]];

SystemItem主要有:

    UIBarButtonSystemItemDone,

    UIBarButtonSystemItemCancel,

    UIBarButtonSystemItemEdit,  

    UIBarButtonSystemItemSave,  

    UIBarButtonSystemItemAdd,

    UIBarButtonSystemItemFlexibleSpace,

    UIBarButtonSystemItemFixedSpace,

    UIBarButtonSystemItemCompose,

    UIBarButtonSystemItemReply,

    UIBarButtonSystemItemAction,

    UIBarButtonSystemItemOrganize,

    UIBarButtonSystemItemBookmarks,

    UIBarButtonSystemItemSearch,

    UIBarButtonSystemItemRefresh,

    UIBarButtonSystemItemStop,

    UIBarButtonSystemItemCamera,

    UIBarButtonSystemItemTrash,

    UIBarButtonSystemItemPlay,

    UIBarButtonSystemItemPause,

    UIBarButtonSystemItemRewind,

    UIBarButtonSystemItemFastForward,

4:使用initWithCustomView

A>加一个开关switch:

[myToolBarItems addObject:[[[UIBarButtonItem alloc] initWithCustomView:[[[UISwitch alloc] init] 

autorelease]];  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值