Xcode/iOS:如何在向下滚动时隐藏导航栏和工具栏?

10

我希望在我在iPhone上向下滚动时隐藏两个栏。当我向上滚动时,它们应该再次出现。我该如何处理?


1
你尝试过在 scrollViewDidScroll: 中使用 setToolbarHidden:animated:setNavigationBarHidden:animated:contentOffset 的 x 值增加时吗? - dasdom
1
请在这里使用英语。这是一个来自世界各地的人们的平台。当我们开始用德语交谈时,许多人无法跟随。如果您需要德语建议,请给我发送电子邮件。我认为,在我的个人资料中可以找到足够的信息来轻松找到我的电子邮件地址。 - dasdom
4个回答

7
- (void)scrollViewWillBeginScroll :(UIScrollView *)scrollView {
      if (scrollView.contentOffset.y < lastOffset.y) {
                 [toolBar setHidden:YES];
                 [[[self navigationController] navigationBar] setHidden:YES];
      } else{
                 // unhide
      }
}

- (void)scrollViewDidScroll :(UIScrollView *)scrollView {
      /// blah blah
      lastOffset = scrollView.contentOffset;
}

注意:lastOffset是一个CGPoint类型的变量,需要在头文件中声明:@Interface。

mdominick: 你说的"iVar"是什么意思? - filou
2
这是一个CGPoint:CGPoint lastOffset; 它放在你的头文件中:@Interface Blah : NSObject { GCPoint lastOffset; } - mdominick
2
我在UIScrollViewDelegate中找不到scrollViewWillBeginScroll方法。 - Shmidt
@Shmidt:请看一下我的答案。 - vikingosegundo
这是声明lastOffset的方式吗?@property (nonatomic) CGPoint lastOffset; - napstercake
显示剩余2条评论

4

对于我来说,被接受的答案并不适用,因为 scrollViewWillBeginScroll: 不是一个委托方法。

相反,我执行以下操作

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldHide" object:self];

}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView 
                 willDecelerate:(BOOL)decelerate
{
    if(!decelerate)
        [[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldUnhide" 
                                                            object:self];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldUnhide"
                                                        object:self];
}

应用程序中的任何对象都可以监听此通知,例如:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"BarsShouldHide" 
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification *note) {
        //hide tab bar with animation;
    }];
    [[NSNotificationCenter defaultCenter] addObserverForName:@"BarsShouldUnhide" 
                                                      object:nil
                                                       queue:nil
                                                  usingBlock:^(NSNotification *note) {
        //Unhide tab bar with animation;
    }];
}

这段代码将隐藏任何滚动条。如果你只想有一个向下的滚动条,那么可以使用与接受的答案中相同的locationOffset技巧。


0

你可以检查一下,它可用于iOS8,我认为这是您要寻找的相反的东西...但是值得检查,因为它是标准的,并且这就是Safari的工作方式。

Swift

var hidesBarsOnSwipe: Bool

Objective-C

@property(nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe Discussion

当将此属性设置为YES时,向上滑动将隐藏导航栏和工具栏。向下滑动会再次显示两个栏。如果工具栏没有任何项,则在滑动后仍然可见。此属性的默认值为NO。


0

这是我的Swift解决方案,它运行得非常好

func scrollViewDidScroll(scrollView: UIScrollView) {
    let navController: UINavigationController = self.navigationController!
    if self.collectionView.panGestureRecognizer.translationInView(self.view).y <= 0.0 {
        defaultCenter.postNotificationName("stuffShouldHide", object: self)
    } else {
        defaultCenter.postNotificationName("stuffShouldUnhide", object: self)
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接