一.導(dǎo)航欄類(lèi)的準(zhǔn)備
1.創(chuàng)建一個(gè)XLNavigationController類(lèi)僧鲁,用于管理導(dǎo)航欄的一些設(shè)置
2.向UIConstants中導(dǎo)入一些顏色設(shè)置
//導(dǎo)航欄的背景顏色
#define kNavigationBarTintColor [UIColor colorWithRed:249/255.0 green:245/255.0 blue:248/255.0 alpha:1];
//按鈕的顏色
#define kNavigationBarItemColor [UIColor colorWithRed:99/255.0 green:102/255.0 blue:111/255.0 alpha:1]
3.導(dǎo)航欄的相關(guān)設(shè)置
- (void)viewDidLoad {
[super viewDidLoad];
//設(shè)置背景顏色
self.navigationBar.barTintColor = kNavigationBarTintColor;
//設(shè)置導(dǎo)航欄標(biāo)題的字體大小和字體顏色
self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor blackColor]};
}
二.工具條類(lèi)的準(zhǔn)備
1.創(chuàng)建XLToolBarView類(lèi)做入,用于充當(dāng)工具條的作用
2..在EnumConstants里面定義一個(gè)block類(lèi)型跃洛,用于回調(diào)工具條哪一個(gè)按鈕被點(diǎn)擊了
//定義一個(gè)block類(lèi)型铁孵,用于自定義toolBar回調(diào)按鈕的事件
typedef void(^ToolBarBlock) (NSInteger index);
3.定義一個(gè)數(shù)組用于接受創(chuàng)建的按鈕信息和用于回調(diào)那個(gè)按鈕被點(diǎn)擊的變量
/**接受外部傳遞的信息*/
@property (nonatomic,copy) NSArray *itemsArray;
/**回調(diào)某個(gè)按鈕被點(diǎn)了*/
@property (nonatomic,copy) ToolBarBlock block;
4.重寫(xiě)initWithFrame方法镜撩,設(shè)置背景顏色
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = kNavigationBarTintColor;
}
return self;
}
5.重寫(xiě)itemsArray的set方法戈二,將按鈕顯示
#pragma mark -------重寫(xiě)itemsArray的set方法 ---------
-(void)setItemsArray:(NSArray *)itemsArray{
_itemsArray = itemsArray;
//計(jì)算按鈕之間的間距
CGFloat kPadding = (SCREEN_WIDTH-itemsArray.count*kSize) / (itemsArray.count+1);
//添加
for (int i = 0; i < itemsArray.count; i++) {
//創(chuàng)建
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//坐標(biāo)
btn.frame = CGRectMake(kPadding+i*(kPadding+kSize), (42-kSize)/2.0, kSize, kSize);
//tag值
btn.tag = i+1;
//賦予圖片
[btn setImage:[UIImage imageNamed:[itemsArray objectAtIndex:i]] forState:UIControlStateNormal];
//添加事件
[btn addTarget:self action:@selector(btnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
//顯示
[self addSubview:btn];
}
}
#pragma mark -------按鈕被點(diǎn)了 ---------
-(void)btnDidClicked:(UIButton *)sender{
if (self.block) {
self.block(sender.tag);
}
}
三.界面類(lèi)的準(zhǔn)備
1.創(chuàng)建XLBaseViewController類(lèi)舒裤,作為各個(gè)界面的父類(lèi)
2.定義兩個(gè)block類(lèi)型,用于正常按鈕和返回按鈕被點(diǎn)擊的回調(diào)事件
//定義的block類(lèi)型
typedef void (^BackBlock)(void);
typedef void (^ItemBlock)(UIButton *item);
3.在EnumConstants里面加入兩個(gè)將要用到的枚舉
//導(dǎo)航欄上添加按鈕的位置
typedef NS_ENUM(NSInteger,kButtonPostionType){
kButtonPostionTypeLeft, //左邊
kButtonPostionTypeRight, //右邊
kButtonPostionTypeNone //不需要位置
};
//工具條的類(lèi)型
typedef NS_ENUM(NSInteger,ToolBarType) {
ToolBarTypeMain, //主界面
ToolBarTypeAlbum, //相冊(cè)
ToolBarTypeBrowser//相冊(cè)瀏覽
};
4.定義一些接口用于相關(guān)設(shè)置
//添加正常按鈕
-(UIBarButtonItem *)addItemWithName:(NSString *)name postion:(kButtonPostionType)postion complish:(ItemBlock)block;
//添加返回按鈕
-(void)addBackItem:(BackBlock)block;
//添加logo
-(void)addLogo;
//添加ToolBar
-(void)showToolBarWithType:(ToolBarType)type handle:(ToolBarBlock)block;
//隱藏ToolBar
-(void)hideToolBar;
5.導(dǎo)入工具條類(lèi)觉吭,定義一些要用到的變量腾供,并設(shè)置默認(rèn)背景顏色
#import "XLBaseViewController.h"
#import "XLToolBarView.h"
@interface XLBaseViewController ()
/**返回按鈕的block屬性變量*/
@property (nonatomic,copy) BackBlock backBlock;
/**左邊按鈕的block屬性變量*/
@property (nonatomic,copy) ItemBlock leftItemBlock;
/**右邊按鈕的block屬性變量*/
@property (nonatomic,copy) ItemBlock rightItemBlock;
/**底部工具欄*/
@property (nonatomic,strong) XLToolBarView *toolBarView;
@end
@implementation XLBaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
//設(shè)置默認(rèn)背景顏色
self.view.backgroundColor = [UIColor whiteColor];
}
6.寫(xiě)一個(gè)方法用來(lái)創(chuàng)建正常按鈕
#pragma mark -------創(chuàng)建按鈕 ---------
-(UIBarButtonItem *)createItemWithTitle:(NSString *)title image:(NSString *)imgName action:(SEL)action postion:(kButtonPostionType)postion{
//獲取圖片
UIImage *img = [UIImage imageNamed:imgName];
//獲取一張可拉伸的圖片,拉伸點(diǎn)為圖片的中心點(diǎn)
UIImage *stretchableImg = [img stretchableImageWithLeftCapWidth:img.size.width/2.0 topCapHeight:img.size.height/2.0];
//創(chuàng)建
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 45, 30)];
//設(shè)置tag值辨別左右鲜滩,0左邊伴鳖,1右邊,參考枚舉
btn.tag = postion;
//設(shè)置圖片
[btn setBackgroundImage:stretchableImg forState:UIControlStateNormal];
//設(shè)置文本
[btn setTitle:title forState:UIControlStateNormal];
//設(shè)置文本顏色
[btn setTitleColor:kNavigationBarItemColor forState:UIControlStateNormal];
//添加事件
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
//創(chuàng)建UIBarItem
UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
return barItem;
}
7.創(chuàng)建正常按鈕和返回按鈕徙硅,并添加方法
#pragma mark -------添加一個(gè)按鈕-給左邊還是右邊榜聂,以及block ---------
-(UIBarButtonItem *)addItemWithName:(NSString *)name postion:(kButtonPostionType)postion complish:(ItemBlock)block{
//創(chuàng)建按鈕
UIBarButtonItem *item = [self createItemWithTitle:name image:nil action:@selector(barBarItemDidClick:) postion:postion];
//判斷創(chuàng)建的按鈕位置
if (postion == kButtonPostionTypeLeft) {
//顯示
self.navigationItem.leftBarButtonItem = item;
//保存block
self.leftItemBlock = block;
}else{
//顯示
self.navigationItem.rightBarButtonItem = item;
//保存block
self.rightItemBlock = block;
}
return item;
}
#pragma mark -------添加返回按鈕 ---------
-(void)addBackItem:(BackBlock)block{
//保存block
self.backBlock = block;
//創(chuàng)建按鈕
UIBarButtonItem *back = [self createItemWithTitle:@"Back" image:nil action:@selector(backBarItemDidClick:) postion:kButtonPostionTypeNone];
//顯示
self.navigationItem.leftBarButtonItem = back;
}
#pragma mark -------正常按鈕觸發(fā)的事件 ---------
-(void)barBarItemDidClick:(UIButton *)sender{
//判斷按鈕的位置-回調(diào)事件
if (sender.tag == kButtonPostionTypeLeft) {
if (self.leftItemBlock) {
self.leftItemBlock(sender);
}
}else{
if (self.rightItemBlock) {
self.rightItemBlock(sender);
}
}
}
#pragma mark -------返回按鈕觸發(fā)的事件 ---------
-(void)backBarItemDidClick:(UIBarButtonItem *)sender{
//回調(diào)事件
if (self.backBlock) {
self.backBlock();
}
//返回上一個(gè)界面
[self.navigationController popViewControllerAnimated:YES];
}
8.導(dǎo)入素材,并實(shí)現(xiàn)添加logo的方法
-(void)addLogo{
//設(shè)置圖片大小
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 19)];
//設(shè)置圖片
imgView.image = [UIImage imageNamed:@"NavigationBar_Logo_normal"];
//顯示
self.navigationItem.titleView = imgView;
}
9.實(shí)現(xiàn)添加工具條和移除工具條的方法
#pragma mark -------添加ToolBar ---------
-(void)showToolBarWithType:(ToolBarType)type handle:(ToolBarBlock)block{
NSArray *itemsArray = nil;
if (type == ToolBarTypeMain) {
//主界面
itemsArray = @[@"select-all",@"delete"];
}else if(type == ToolBarTypeAlbum){
//相冊(cè)界面
itemsArray = @[@"select-all",@"copy",@"cut",@"delete"];
}else{
//相冊(cè)瀏覽界面
itemsArray = @[@"delete",@"more"];
}
//創(chuàng)建toolBar
self.toolBarView = [[XLToolBarView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 42)];
//接受數(shù)據(jù)
_toolBarView.itemsArray = itemsArray;
//保存block
_toolBarView.block = block;
//顯示
[self.view addSubview:_toolBarView];
[UIView animateWithDuration:0.5 animations:^{
_toolBarView.y = SCREEN_HEIGHT-42;
}];
}
#pragma mark -------隱藏ToolBar ---------
-(void)hideToolBar{
[UIView animateWithDuration:0.5 animations:^{
_toolBarView.y = SCREEN_HEIGHT;
} completion:^(BOOL finished) {
[_toolBarView removeFromSuperview];
}];
}