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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10180446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:59:26  来源:igfitidea点击:

Xcode/iOS: How to hide Navigation- AND ToolBar on scroll down?

iosxcodetoolbarnavigationbar

提问by filou

I would like to hide both bars on scroll down on my iPhone. When I scroll up, they should appear again.. How can I handle this?

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

采纳答案by vikingosegundo

The accepted answer does not work for me, as scrollViewWillBeginScroll:is not a delegate method.

接受的答案对我不起作用,因为scrollViewWillBeginScroll:它不是委托方法。

Instead I do

相反,我做

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

Anywhere in the app objects can listen for this notification,like

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

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

This code will hide the bars for any scroll. if you want to have only on down, the same locationOffsettrick as in the accepted answer should work.

此代码将隐藏任何滚动条。如果你只想放下,与locationOffset接受的答案相同的技巧应该有效。

回答by mdominick

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

Note: lastOffsetis an CGPointand it goes in your header file: @Interface.

注意lastOffset是一个CGPoint,它在你的头文件中:@Interface

回答by Grant Park

Here's my solution in Swift; it works beautifully

这是我在 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)
    }
}

回答by anoop4real

You might check this, available from iOS8, i think this is the reverse of what you are looking for...but worth checking as it is something standard and this is how Safari works.

您可能会检查这个,可从 iOS8 获得,我认为这与您正在寻找的相反......但值得检查,因为它是标准的东西,这就是 Safari 的工作方式。

Swift

var hidesBarsOnSwipe: Bool

Objective-C

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

When this property is set to YES, an upward swipe hides the navigation bar and toolbar. A downward swipe shows both bars again. If the toolbar does not have any items, it remains visible even after a swipe. The default value of this property is NO.

迅速

var hidesBarsOnSwipe: Bool

目标-C

@property(nonatomic, readwrite,assign) BOOL hidesBarsOnSwipe 讨论

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