
公司突然要求有这个功能,然后就写一个,顺便说说其中的技巧和坑~
这个其实有集中方法,我就说我使用的,如果读者有更好的,请告诉我,我也学习一下
步骤
1.关闭tableView的自适应属性
2.在viewWillAppear
方法中去设置系统自带的导航条隐藏
3.生成一个导航条,设置他的属性
4.给tableView添加KVO
5.在KVO中给自定义的导航条设置颜色的更改
1.关闭tableView(scrollView或者他的子类)的自适应属性
//控制器自带属性
self.edgesForExtendedLayout = UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets = NO;
2.在
viewWillAppear
方法中去设置系统自带的导航条隐藏
#pragma mark - 3.控制器的声明周期
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO];
}
3.生成一个导航条,设置他的属性
/**给定一个假的naBar*/
@property(strong,nonatomic)UINavigationBar *tempNavigationBar;
- (UINavigationBar *)tempNavigationBar{
if (!_tempNavigationBar) {
_tempNavigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
_tempNavigationBar.translucent = NO;
[_tempNavigationBar setTintColor:[UIColor whiteColor]];
}
return _tempNavigationBar;
}
3.5 设置导航栏的左右item
/**
* 设置导航栏的内容,这里要做的就是给我们自定义的tempNavigationBar添加左右Item
*/
- (void)setupNavBar
{
//添加假的bar
[self.view addSubview:self.tempNavigationBar];
UINavigationItem *tempNavigationItem = [[UINavigationItem alloc] initWithTitle:@""];
//其实这个位置,也可以写一个临时变量 navItem,不用tempNavigationItem这个全局属性也行,刚才我给他起名字的时候,叫做navgationItem,我忘了和系统的名字一样了,一直出现问题,最后找到了才解决,所以千万别像我似的搞成这个样子
self.tempNavigationItem = tempNavigationItem;
// 设置导航栏按钮
//我刚才使用了navgationItem ,就出现问题了,我就纳闷,最后才发现是使用了系统自带的属性
UIBarButtonItem *findItem = [UINavigationItem barButtonItem:[UIImage imageNamed:@"personcenter_navbar_search"]
target:self
action:@selector(findItemClick)];//跳转到个股查询;
UIBarButtonItem *moreItem = [UINavigationItem barButtonItem:[UIImage imageNamed:@"navigationbar_more"]
target:self
action:@selector(alterClick)];
if(self.user.isMe)//如果是我
{ //先用着数组,因为以后可能有两个item一起
[self.tempNavigationItem setRightBarButtonItem:findItem];
}else{
//不是我
[self.tempNavigationItem setRightBarButtonItem:moreItem];
}
// 设置导航栏按钮
UIBarButtonItem *backItem = [UINavigationItem barButtonItem:[UIImage imageNamed:@"fanhui"]
target:self
action:@selector(popVCAction)];
self.tempNavigationItem.leftBarButtonItem = backItem;
[self.tempNavigationBar setItems:@[self.tempNavigationItem] animated:NO];
//设置假bar的透明度
[self.tempNavigationBar setTranslucent:YES];
[self.tempNavigationBar setShadowImage:[UIImage new]];
[self.tempNavigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}
4.给tableView添加KVO
//添加观察者
[self.tableView addObserver:self
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionInitial
context:nil];
//一定要移除他,否则会出现各种问题,还不报错~
- (void)dealloc{
[SENotificationCenter removeObserver:self];
[self.tableView removeObserver:self forKeyPath:@"contentOffset"];
}
5.监听变化
#pragma mark - 5.设置通知,观察者,navigation,items,tableview等基本属性
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentOffset"]) {
[self.tempNavigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
UIColor *navBgColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:(self.tableView.contentOffset.y / 100)];
[self.tempNavigationBar lt_setBackgroundColor:navBgColor];
if (self.tableView.contentOffset.y / 100 >= 1) {
[self.tempNavigationBar setShadowImage:nil];
}
else{
[self.tempNavigationBar setShadowImage:[UIImage new]];
}
}
}
lt_setBackgroundColor
这个是一个第三方,专门改变颜色的,挺好用#import "UINavigationBar+Awesome.h"
,去搜索一下就行
网友评论