- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self drawNavi];
[self drawMainView];
//设置右键大小
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:13],NSFontAttributeName, nil] forState:UIControlStateNormal];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//左侧建
[self setBackLeftBtn];
//去掉背景色和下面的线
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
//初始化导航栏颜色和左右键颜色
[self setNavigationBarChangeWhite:YES];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
//去掉导航栏设置的左键
[self.leftBtn removeFromSuperview];
//还原设定
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar setShadowImage:nil];
[self setNavigationBarChangeWhite:NO];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//先获取y轴偏移
CGFloat yOffset = scrollView.contentOffset.y;
//设置透明度
CGFloat alpha = fabs(yOffset)/kTopHeight;
if (alpha<0.5) {
[self setNavigationBarChangeWhite:YES];
} else {
[self setNavigationBarChangeWhite:NO];
}
//设置导航栏的背景图片(将颜色生成的图片)
//imageWithColor为扩展的方法根据颜色生成图片
[self.navigationController.navigationBar setBackgroundImage:[UIColor imageWithColor:[[UIColor whiteColor]colorWithAlphaComponent:alpha]] forBarMetrics:UIBarMetricsDefault];
}
-(void)setNavigationBarChangeWhite:(BOOL)isChange{
if(isChange){
//设置自定义的左键颜色
self.leftBtn.nameL.textColor = k_white_color;
//设置右键的颜色(复用导航栏的左右键时候)
[self.navigationController.navigationBar setTintColor:k_white_color];
//设置导航栏title的颜色
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:k_white_color,NSFontAttributeName:TEXT_MIDD_B_FONT16}];
}else{
//设置自定义的左键颜色
self.leftBtn.nameL.textColor = k_title_color;
//设置导航栏title的颜色
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:k_title_color,NSFontAttributeName:TEXT_MIDD_B_FONT16}];
//设置右键的颜色(复用导航栏的左右键时候)
[self.navigationController.navigationBar setTintColor:k_title_color];
}
}