gpt4 book ai didi

ios - 像 Instagram iOS 应用一样自动隐藏和显示 UINavigationBar

转载 作者:行者123 更新时间:2023-11-28 19:40:24 25 4
gpt4 key购买 nike

我有这段代码:

- (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;

}

这给出了隐藏我的导航栏的预期效果,但导航只会在您滚动到 ScrollView 的顶部时重新出现(y 偏移量 = 0)。我怎样才能重现 Instagram 的行为,每当您向上滚动时,导航栏就会重新出现?

最佳答案

我已经放弃了这个更直观的代码的手动框架代码:

- (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];
}

}

这还添加了一个 10px 的阈值,以便它只对有意义的向上或向下滚动使用react

关于ios - 像 Instagram iOS 应用一样自动隐藏和显示 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34636462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com