首先和导航控制器的定制原理是一样的,首先要搞清楚toolBar是属于导航控制器管理的
,所以在定制toolBar的时候,要在拿到导航控制器才可以定制
而toolBarItem是属于视图控制器的,要拿到这个视图控制器才可以定制
FJNavigationController.m
#import "FJNavigationController.h"
@interface FJNavigationController ()
@end
@implementation FJNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self toolBarSetting];
}
#pragma mark -定制toolBar
- (void)toolBarSetting{
//toolBar默认是隐藏的
self.toolbarHidden = NO;//一般只在需要显示工具条的节目去设置
//1.设置是否有透明度(默认是有透明度的)
self.toolbar.translucent = NO;
//2.设置toolBar颜色
[self.toolbar setBarTintColor:[UIColor cyanColor]];
//3.设置填充颜色(镂空部分的颜色)
[self.toolbar setTintColor:[UIColor cyanColor]];
//4.设置背景图片
//参数1:图片
//参数2:显示位置
//参数3:度量
[self.toolbar setBackgroundImage:[UIImage imageNamed:@"toolBar"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault] ;
}
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self.view setBackgroundColor:[UIColor lightGrayColor]];
//定制toolBarItem
[self toolbarItemSetting];
}
#pragma mark - 定制toolbarItem
- (void)toolbarItemSetting{
//toolBarItem中得item和navigationItem中得item属于同一类
//都是UIBarButtonItem;
//1.创建toolbarItem
UIBarButtonItem *item1 =[ [UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(onClicked)];
UIBarButtonItem *item2 =[ [UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(onClicked)];
UIBarButtonItem *item3 =[ [UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:@selector(onClicked)];
//3.将items均匀的显示在toolBar
//专门用来占位item(自动计算两个item之间的间隔)
// UIBarButtonSystemItemFlexibleSpace,
//只是用来占位,不需要点击
// UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]
// initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//
//固定两个item之间的间隔:
// UIBarButtonSystemItemFixedSpace,
UIBarButtonItem *spaceItem2 =[ [UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
spaceItem2.width = 120;
self.toolbarItems = @[spaceItem2,item1,spaceItem2,item2,spaceItem2,item3,spaceItem2];
//2.显示在toolBar上
// self.toolbarItems = @[spaceItem,item1,spaceItem,item2,spaceItem,item3,spaceItem];
}
#pragma mark -点击toolBar的item
- (void) onClicked{
NSLog(@"被点击了");
}
@end