ios 代码创建UIToolBar

本文介绍如何使用UIKit框架中的UIToolbar组件来定制iOS应用的工具栏。具体步骤包括:创建工具栏实例、设置工具栏尺寸、添加左/右两侧按钮,并调整背景样式等。

分为4步骤


1、创建一个UIToolbar

2、创建一个NSMutableArray *buttons 用来存放UIBarButtonItem  

3、buttons addObject 添加  按钮

4、[self.toolBar setItems:button];

5、self.view 添加toolbar

代码如下

                      action:NULL];
    //不限制大小 自适应
    self.toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 20, width, 0)];
    
    //左侧取消按钮
    UIBarButtonItem * leftBtn=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    //右侧发布按钮
    UIBarButtonItem * rightBtn=[[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(fabu:)];
    NSMutableArray * buttons=[[NSMutableArray alloc]initWithCapacity:4];
    [buttons addObject:leftBtn];
    [buttons addObject:flexibleSpaceItem1];
    [buttons addObject:rightBtn];
    [self.toolBar setItems:buttons];
    [self.toolBar sizeToFit];
    
    
        self.toolBar.backgroundColor=color;
        self.toolBar.tintColor=[UIColor whiteColor];
        [self.toolBar setBackgroundImage:[UIImage new]forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
        [self.toolBar setShadowImage:[UIImage new]
                 forToolbarPosition:UIToolbarPositionAny];
    [self.view addSubview:self.toolBar];


转载于:https://my.oschina.net/u/554046/blog/625071

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值