Xcode / iOS:如何在向下滾動時隱藏Navigation- AND ToolBar?

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


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上向下滾動隱藏兩個欄。當我向上滾動時,它們應該再次出現..我該如何處理?

4 个解决方案

#1


4  

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

app對象中的任何位置都可以監聽此通知,例如

- (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 locationOffset trick as in the accepted answer should work.

此代碼將隱藏任何滾動條。如果你想只關閉,那么在接受的答案中應該使用相同的locationOffset技巧。

#2


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

Note : lastOffset is an CGPoint and it goes in your header file: @Interface.

注意:lastOffset是一個CGPoint,它位於頭文件中:@Interface。

#3


0  

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)
    }
}

#4


0  

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

var hidesBarsOnSwipe:Bool

Objective-C

Objective-C的

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

@property(非原子,readwrite,assign)BOOL hidesBarsOnSwipe討論

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.

當此屬性設置為YES時,向上滑動會隱藏導航欄和工具欄。向下滑動再次顯示兩個條形。如果工具欄沒有任何項目,即使在滑動后它仍然可見。此屬性的默認值為NO。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/04/16/7255d948c13592eecb3955a1e85eb047.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com