1.導(dǎo)航欄必須在滾動(dòng)視圖控制器中
2.設(shè)置一個(gè)屬性,表示視圖向上還是向下
3.調(diào)用UIScrollViewDelegate中的代理方法-(void)scrollViewDidScroll:scrollView
//UIScrollView的代理方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{ //定義起初 Y 軸偏移量
static float newy = 0;
static float oldy = 0;
//獲取當(dāng)前滾動(dòng)視圖的contentOffset.y
newy = self.tableView.contentOffset.y;
//判斷滾動(dòng)視圖向上還是向下
if (newy != oldy ) {
if (newy > oldy) {
self.scrollUporDown = YES;
}else if(newy < oldy){
self.scrollUporDown = NO;
}
oldy = newy;
}
//定義初始的navigationBar的位置
if (newy == -60) {
[UIView animateWithDuration:1 animations:^{
self.navigationController.navigationBar.frame = CGRectMake(0, 20, kWidth, 40);
}];
}else{
if (_scrollUporDown == YES) {
[UIView animateWithDuration:1 animations:^{
self.navigationController.navigationBar.frame = CGRectMake(0, -40, kWidth, 40);
}];
}
else if (_scrollUporDown == NO) {
[UIView animateWithDuration:0.5 animations:^{
self.navigationController.navigationBar.frame = CGRectMake(0, 20, kWidth, 40);
}];
}
}
}
本demo是在項(xiàng)目開(kāi)發(fā)中寫(xiě)的一個(gè)方法,可能網(wǎng)上有很多實(shí)現(xiàn)這種功能的方法,但是這也是自己親身實(shí)踐所取得的結(jié)果,希望大神們勿噴,謝謝!
有什么不對(duì)的地方希望大神們指出!