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的介绍到这里就结束了。