目前我有一个UICollection
,当您向下滚动40px时,我会隐藏UINavigation
顶栏。当您向上滚动到顶部时,UINavigation
顶栏会再次出现。
但是我希望能够在用户滚动40px时显示/隐藏UINavigation
顶栏,而不仅仅是当您从顶部滚动时。
例如,当您从顶部开始滚动时,栏会隐藏,您可以滚动到UICollectionView的中间位置,然后当您通过滚动回到顶部时,UINavigation
顶栏会再次显示。
有什么想法吗?
#pragma mark - UIScrollViewDelegate Methods
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
return;
}
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
return;
}
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height - 300;
if (offsetY > contentHeight - scrollView.frame.size.height)
{
[self.homePaginator fetchNextPage];
}
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 25;
if([scrollView.panGestureRecognizer translationInView:self.view].y < 0)
{
frame.origin.y = -size;
if(self.navigationController.navigationBar.items.count > 0){
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGFloat navBarHeight = 25.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
}
completion:^(BOOL finished) {
self.navigationItem.titleView.alpha = 0;
self.piccingTitleIcon.alpha = 0;
self.navigationItem.rightBarButtonItem = nil;
}];
}
}
else if([scrollView.panGestureRecognizer translationInView:self.view].y > 0)
{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGFloat navBarHeight = 64.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
self.navigationItem.titleView.alpha = 1;
self.piccingTitleIcon.alpha = 1;
self.btnSearch = [[UIBarButtonItem alloc] initWithCustomView:self.searchIconButton];
[self.navigationItem setRightBarButtonItem:self.btnSearch];
}
completion:^(BOOL finished) {
[self performSelector:@selector(addIconsToNavBar) withObject:nil afterDelay:-1.0];
}];
}
}
答案 0 :(得分:1)
试试这个!
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGPoint scrollOffset = scrollView.contentOffset;
if (scrollOffset.y >= 40) {
if (![self.navigationController isNavigationBarHidden]) {
[self.navigationController setNavigationBarHidden:YES animated:YES];
}
} else {
if ([self.navigationController isNavigationBarHidden]) {
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
}
答案 1 :(得分:1)
在用户滚动之前获取起始内容偏移量。
– scrollViewWillBeginDragging:(UIScrollView *)scrollView {
currentOffset = scrollView.contentOffset.y;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat scrollPos = scrollView.contentOffset.y - currentOffset;
if(scrollPos >= 40 || scrollPos <= -40 /* or whatever the height of your toolbar is */){
//Fully hide your toolbar
[UIView animateWithDuration:2.25 animations:^{
[self.navigationController setNavigationBarHidden:YES animated:YES];
}];
} else {
//Slide it up incrementally, etc.
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
答案 2 :(得分:0)
任何想要了解简单方法的人都可以使用以下代码。
有一些库可以很容易地做到这一点,但我发现我必须用他们的自定义类UINavgationController
重写应用程序的导航如果你有一个现有的项目,你可以更容易地做到这一点...... / p>
#pragma mark - UIScrollViewDelegate Methods
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
return;
}
CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
return;
}
CGFloat offsetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height - 300;
if (offsetY > contentHeight - scrollView.frame.size.height)
{
[self.homePaginator fetchNextPage];
}
CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 25;
if([scrollView.panGestureRecognizer translationInView:self.view].y < 0)
{
frame.origin.y = -size;
if(self.navigationController.navigationBar.items.count > 0){
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGFloat navBarHeight = 25.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
}
completion:^(BOOL finished) {
self.navigationItem.titleView.alpha = 0;
self.piccingTitleIcon.alpha = 0;
self.navigationItem.rightBarButtonItem = nil;
}];
}
}
else if([scrollView.panGestureRecognizer translationInView:self.view].y > 0)
{
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
CGFloat navBarHeight = 64.0f;
CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
[self.navigationController.navigationBar setFrame:frame];
self.navigationItem.titleView.alpha = 1;
self.piccingTitleIcon.alpha = 1;
self.btnSearch = [[UIBarButtonItem alloc] initWithCustomView:self.searchIconButton];
[self.navigationItem setRightBarButtonItem:self.btnSearch];
}
completion:^(BOOL finished) {
[self performSelector:@selector(addIconsToNavBar) withObject:nil afterDelay:-1.0];
}];
}
}