像 Instagram iOS 应用一样自动隐藏和显示 UINavigationBar
auto hiding and showing UINavigationBar like Instagram iOS app
我有这段代码:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UINavigationBar *navbar =self.navigationController.navigationBar;
UIView *tableView = self.view;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
CGRect tableFrame = self.view.frame;
//changing the origin.y based on the current scroll view.
//Adding +20 for the Status Bar since the offset is tied into that.
navBarFrame.origin.y = MIN(0, MAX(-44, (scrollView.contentOffset.y * -1))) +20 ;
tableFrame.origin.y = navBarFrame.origin.y + navBarFrame.size.height;
navbar.frame = navBarFrame;
tableView.frame = tableFrame;
}
这提供了隐藏我的导航栏的预期效果,但只有当您滚动到滚动视图的顶部(y 偏移量 = 0)时,导航才会重新出现。我怎样才能重现 Instagram 的行为,每当您向上滚动时,导航栏就会重新出现?
使用此 link 检测滚动方向:Detect Scroll Direction
我真的不明白你用你发布的代码做了什么。您是每次都创建一个新的导航栏还是隐藏和显示相同的导航栏?
无论如何,一旦检测到滚动方向,就在 ScrollDirection = ScrollDirectionUp
时显示导航栏。
类似于:
if (ScrollDirection == ScrollDirectionUp) {
self.navigationController.navigationBar.hidden = NO;
}
我已经放弃了这个更直观的代码的手动框架代码:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (lastContentOffset > scrollView.contentOffset.y) {
if (downwards) {
downwards = NO;
scrollDistance = 0;
} else {
scrollDistance++;
}
}
else if (lastContentOffset < scrollView.contentOffset.y) {
if (!downwards) {
downwards = YES;
scrollDistance = 0;
} else {
scrollDistance++;
}
}
lastContentOffset = scrollView.contentOffset.y;
CGFloat threshold = 10;
if (downwards && !self.navigationController.navigationBarHidden && scrollDistance > threshold) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (!downwards && self.navigationController.navigationBarHidden && scrollDistance > threshold) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
这还会添加 10 像素的阈值,以便它仅对有意义的向上或向下滚动做出反应
我有这段代码:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
UINavigationBar *navbar =self.navigationController.navigationBar;
UIView *tableView = self.view;
CGRect navBarFrame = self.navigationController.navigationBar.frame;
CGRect tableFrame = self.view.frame;
//changing the origin.y based on the current scroll view.
//Adding +20 for the Status Bar since the offset is tied into that.
navBarFrame.origin.y = MIN(0, MAX(-44, (scrollView.contentOffset.y * -1))) +20 ;
tableFrame.origin.y = navBarFrame.origin.y + navBarFrame.size.height;
navbar.frame = navBarFrame;
tableView.frame = tableFrame;
}
这提供了隐藏我的导航栏的预期效果,但只有当您滚动到滚动视图的顶部(y 偏移量 = 0)时,导航才会重新出现。我怎样才能重现 Instagram 的行为,每当您向上滚动时,导航栏就会重新出现?
使用此 link 检测滚动方向:Detect Scroll Direction
我真的不明白你用你发布的代码做了什么。您是每次都创建一个新的导航栏还是隐藏和显示相同的导航栏?
无论如何,一旦检测到滚动方向,就在 ScrollDirection = ScrollDirectionUp
时显示导航栏。
类似于:
if (ScrollDirection == ScrollDirectionUp) {
self.navigationController.navigationBar.hidden = NO;
}
我已经放弃了这个更直观的代码的手动框架代码:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (lastContentOffset > scrollView.contentOffset.y) {
if (downwards) {
downwards = NO;
scrollDistance = 0;
} else {
scrollDistance++;
}
}
else if (lastContentOffset < scrollView.contentOffset.y) {
if (!downwards) {
downwards = YES;
scrollDistance = 0;
} else {
scrollDistance++;
}
}
lastContentOffset = scrollView.contentOffset.y;
CGFloat threshold = 10;
if (downwards && !self.navigationController.navigationBarHidden && scrollDistance > threshold) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else if (!downwards && self.navigationController.navigationBarHidden && scrollDistance > threshold) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
这还会添加 10 像素的阈值,以便它仅对有意义的向上或向下滚动做出反应