ios基础篇(十二)——UINavgationController的使用(三)ToolBar

本文详细介绍了如何在UINavigationController中显示并定制UIToolBar,包括设置其外观、添加按钮以及自定义ToolBar的过程。通过实例演示了如何通过代码实现这些功能,适合iOS开发人员学习。

UIToolBar存在于UINavigationController导航栏控制器中,而且默认被隐藏;设置UINavigationController的toolbarHidden属性可显示UIToolBar。

一、UIToolBar的设置

1、在RootViewController.m的viewDidLoad方法中添加代码;

 

[self.navigationController setToolbarHidden:NO animated:YES];

 

如图:显示底部ToolBar

 

2、设置UIToolBar属性:(ios7之后使用)

 

    //设置痕迹颜色
    [self.navigationController.toolbar setBarTintColor:[UIColor greenColor]];

 

如图:

 

//设置背景图片
    [self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"40-15092Z94009"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
    

如图:

3、创建UIBarButtonItem
 UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:nil action:nil];
    
    UIBarButtonItem *center = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:nil action:nil];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil];
    
    //空白
    UIBarButtonItem *blank = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
4、为UIToolBar添加UIBarButtonItem

1)创建一个数组itemsArray;

2)数组上添加UIBarButtonItem;

3)将itemsArray传给UIToolBar;

    NSArray *itemsArray = @[left, blank, center, blank, right];
    [self setToolbarItems:itemsArray];

如图:

 

二、自定义UIToolBar

1、隐藏系统ToolBar:在RootViewController.m 的viewDidLoad方法中添加

    [self.navigationController setToolbarHidden:YES animated:YES];

把新建的Toolbar添加的视图中,并为Toobar设置一个Item

 
    UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:nil action:nil];
    
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@""] style:UIBarButtonItemStylePlain target:nil action:nil];
    
    NSArray *buttons = @[one,two];
    
    toolBar = [[UIToolbar alloc] initWithFrame:(CGRect){0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0}];
    
    toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [toolBar setBarStyle:UIBarStyleDefault];
    [toolBar setItems:buttons];
    [self.view addSubview:toolBar];
    

 UINavigationController的介绍到这里就结束了。

转载于:https://www.cnblogs.com/0320y/p/5047939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值