首先和導航控制器的定製原理是同樣的,首先要搞清楚toolBar是屬於導航控制器管理的工具
,因此在定製toolBar的時候,要在拿到導航控制器才能夠定製spa
而toolBarItem是屬於視圖控制器的,要拿到這個視圖控制器才能夠定製.net
FJNavigationController.m3d
#import "FJNavigationController.h"orm
@interface FJNavigationController ()圖片
@end get
@implementation FJNavigationControllerit
- (void)viewDidLoad {io
[super viewDidLoad];class
// 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] ;
}
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