iOS导航栏渐变NavigationController
2016-12-12 本文已影响398人
郑州程序员王一
工作中的一些代码记录
导航栏颜色渐变示例:
颜色渐变.gif//核心代码
//添加背景view
self.navView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, WYScreenW, 64)];
self.navViewColor = WYRGBColorAlpha(74, 159, 242, 0);
self.navView.backgroundColor = self.navViewColor;
[self.navigationController.navigationBar addSubview:self.navView];
//滑动监听
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offset_Y = scrollView.contentOffset.y;
CGFloat alpha = offset_Y / 116.0f;
if (alpha >= 0.1) {
self.titleLabel.hidden = NO;
}else{
self.titleLabel.hidden = YES;
}
self.navView.backgroundColor = [self.navViewColor colorWithAlphaComponent:alpha];
}
导航栏顶部隐藏和显示:
颜色渐变.gif//滑动监听
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offset_Y = scrollView.contentOffset.y;
if(offset_Y > -60)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}else{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
导航栏上拉、下拉 隐藏和显示:
颜色渐变.gifself.automaticallyAdjustsScrollViewInsets = NO;
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, self.view.bounds.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.backgroundColor = WYGlobalBg;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:CellID];
[self.view addSubview:self.tableView];
if([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)])
{
self.automaticallyAdjustsScrollViewInsets = NO;
UIEdgeInsets insets = self.tableView.contentInset;
insets.top =self.navigationController.navigationBar.bounds.size.height;
self.tableView.contentInset =insets;
self.tableView.scrollIndicatorInsets = insets;
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
if(velocity.y>=0)
{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}else
{
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
navigationController
先前的外包瞎乱写,造成一系列接手后的屎坑,这里记录下修改的UInavigation代码
创建,销毁的时候使用系统的navigationController
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
设置导航栏为透明色
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
左右侧按钮
//隐藏原生返回按钮
self.navigationItem.hidesBackButton = YES;
UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
//按钮偏移
backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
[backButton setImage:[UIImage imageNamed:@"back_black"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(clickLeftBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView: backButton];
self.navigationItem.leftBarButtonItem = leftItem;