
navBar隨著tableview的改變而改變
公司突然要求有這個功能,然后就寫一個,順便說說其中的技巧和坑~
這個其實有集中方法,我就說我使用的,如果讀者有更好的,請告訴我,我也學習一下
步驟
1.關(guān)閉tableView的自適應(yīng)屬性
2.在viewWillAppear方法中去設(shè)置系統(tǒng)自帶的導航條隱藏
3.生成一個導航條,設(shè)置他的屬性
4.給tableView添加KVO
5.在KVO中給自定義的導航條設(shè)置顏色的更改
1.關(guān)閉tableView(scrollView或者他的子類)的自適應(yīng)屬性
//控制器自帶屬性
self.edgesForExtendedLayout = UIRectEdgeAll;
self.automaticallyAdjustsScrollViewInsets = NO;
2.在
viewWillAppear方法中去設(shè)置系統(tǒng)自帶的導航條隱藏
#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.生成一個導航條,設(shè)置他的屬性
/**給定一個假的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 設(shè)置導航欄的左右item
/**
* 設(shè)置導航欄的內(nèi)容,這里要做的就是給我們自定義的tempNavigationBar添加左右Item
*/
- (void)setupNavBar
{
//添加假的bar
[self.view addSubview:self.tempNavigationBar];
UINavigationItem *tempNavigationItem = [[UINavigationItem alloc] initWithTitle:@""];
//其實這個位置,也可以寫一個臨時變量 navItem,不用tempNavigationItem這個全局屬性也行,剛才我給他起名字的時候,叫做navgationItem,我忘了和系統(tǒng)的名字一樣了,一直出現(xiàn)問題,最后找到了才解決,所以千萬別像我似的搞成這個樣子
self.tempNavigationItem = tempNavigationItem;
// 設(shè)置導航欄按鈕
//我剛才使用了navgationItem ,就出現(xiàn)問題了,我就納悶,最后才發(fā)現(xiàn)是使用了系統(tǒng)自帶的屬性
UIBarButtonItem *findItem = [UINavigationItem barButtonItem:[UIImage imageNamed:@"personcenter_navbar_search"]
target:self
action:@selector(findItemClick)];//跳轉(zhuǎn)到個股查詢;
UIBarButtonItem *moreItem = [UINavigationItem barButtonItem:[UIImage imageNamed:@"navigationbar_more"]
target:self
action:@selector(alterClick)];
if(self.user.isMe)//如果是我
{ //先用著數(shù)組,因為以后可能有兩個item一起
[self.tempNavigationItem setRightBarButtonItem:findItem];
}else{
//不是我
[self.tempNavigationItem setRightBarButtonItem:moreItem];
}
// 設(shè)置導航欄按鈕
UIBarButtonItem *backItem = [UINavigationItem barButtonItem:[UIImage imageNamed:@"fanhui"]
target:self
action:@selector(popVCAction)];
self.tempNavigationItem.leftBarButtonItem = backItem;
[self.tempNavigationBar setItems:@[self.tempNavigationItem] animated:NO];
//設(shè)置假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];
//一定要移除他,否則會出現(xiàn)各種問題,還不報錯~
- (void)dealloc{
[SENotificationCenter removeObserver:self];
[self.tableView removeObserver:self forKeyPath:@"contentOffset"];
}
5.監(jiān)聽變化
#pragma mark - 5.設(shè)置通知,觀察者,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",去搜索一下就行