2016.10.19
特別說(shuō)明:本標(biāo)題欄樣式部分參照github上的bacy/titlebar
PS:謝謝網(wǎng)上諸多大神資料
效果圖:關(guān)注標(biāo)題就可以啦
- 對(duì)象的定義
@interface ViewController ()<UIAlertViewDelegate,MyViewDelegate>
//需要操作的按鍵需要定義出來(lái)
@property (strong, nonatomic) UIBarButtonItem *leftBtn;
@property (strong, nonatomic) UIBarButtonItem *rightBtn2;
@property (strong, nonatomic) UIBarButtonItem *rightBtn;
@end
- 定義一個(gè)函數(shù)菩暗,專門用來(lái)生成標(biāo)題欄
-(void)NavigationBarInit{
//創(chuàng)建一個(gè)導(dǎo)航欄
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44+20)];
//標(biāo)題欄的整個(gè)背景色改為藍(lán)色
[navigationBar setBarTintColor:[UIColor colorWithRed:64/255.0 green:180/255.0 blue:255/255.0 alpha:1.0]];
//將標(biāo)題欄中間的主title的文字顏色設(shè)置為白色
[navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
//此段設(shè)置整個(gè)標(biāo)題欄的背景圖片
// [navigationBar setBackgroundImage:[UIImage imageNamed:@"xx"] forBarMetrics:UIBarMetricsDefault];
//設(shè)置左右兩邊的圖片和文字均為白色
[navigationBar setTintColor:[UIColor whiteColor]];
//將導(dǎo)航欄添加入view中
[self.view addSubview:navigationBar];
//新建一個(gè)navigationItem
UINavigationItem *navigationItem = [[UINavigationItem alloc] init];
//title為中間的顯示,可以是view捆蜀,可以是文字等等
// navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
// navigationItem.titleView.backgroundColor = [UIColor yellowColor];
//標(biāo)題欄中間的文字
navigationItem.title = @"主菜單";
//標(biāo)題欄左側(cè)一個(gè)按鈕+文字
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
//btn.backgroundColor=[UIColor clearColor];
btn.frame = view.frame;
[btn setImage:[UIImage imageNamed:@"back_small"] forState:UIControlStateNormal];
[btn setTitle:@"返回" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn];
_leftBtn = [[UIBarButtonItem alloc]initWithCustomView:view];
navigationItem.leftBarButtonItem = _leftBtn;
//標(biāo)題欄右側(cè),兩個(gè)按鈕
UIButton *btnR1 = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIButton *btnR2 = [UIButton buttonWithType:UIButtonTypeInfoLight];
btnR1.frame = CGRectMake(0, 0, 20, 20);
btnR2.frame = CGRectMake(0, 0, 20, 20);
[btnR1 setImage:[UIImage imageNamed:@"fabu"] forState:UIControlStateNormal];
[btnR2 setImage:[UIImage imageNamed:@"collect"] forState:UIControlStateNormal];
_rightBtn = [[UIBarButtonItem alloc] initWithCustomView:btnR1];
_rightBtn2 = [[UIBarButtonItem alloc] initWithCustomView:btnR2];
navigationItem.rightBarButtonItems=@[_rightBtn,_rightBtn2];
[navigationBar pushNavigationItem:navigationItem animated:YES];
}
- 最后在viewDidLoad調(diào)用NavigationBarInit
- (void)viewDidLoad {
[super viewDidLoad];
//調(diào)用
[self NavigationBarInit];
}