xcode 滚动uiwebview时隐藏和显示导航栏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21059705/
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-15 04:32:23  来源:igfitidea点击:

hide and show navbar when scroll uiwebview

iphoneobjective-cxcodenavbar

提问by user2966615

i need help, i am trying to build a app in which i have a viewcontrollerwith a uiwebviewand a navbarwith 2 buttons on it. what i want to do is that when user scroll uiwebviewnavbar automatically hides like slid up sort of. but is not working they way i want it to work. let me post code here. In viewdidloadi put this.

我需要帮助,我正在尝试构建一个应用程序,其中有一个viewcontroller带 auiwebview和一个navbar带 2 个按钮的应用程序。我想要做的是当用户滚动uiwebview导航栏时会自动隐藏起来,就像向上滑动一样。但没有按照我希望的方式工作。让我在这里发布代码。在viewdidload我把这个。

[webPage.scrollView setDelegate:self];

and then i have this method

然后我有这个方法

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {
    if(scrollView.contentOffset.y == 0) {
        //show
        NSLog(@"Show");
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    } else {
        NSLog(@"Hide");
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        //hide
    }
}

it NSLogcorrectly but nothing else navbarstill stays. :(

NSLog正确但没有其他东西navbar仍然存在。:(

采纳答案by iOS Dev

You can try this:

你可以试试这个:

1.Declare the navigation bar, a constant with your navigation bar height and 2 BOOL variables:

1.声明导航栏,一个带有导航栏高度的常量和 2 个 BOOL 变量:

UINavigationBar *navBar;

static const CGFloat kNavBarHeight = 60.0f;

BOOL webViewScrollIsDragging;
BOOL webViewScrollIsDecelerating;

2.In viewDidLoad write the following:

2.在 viewDidLoad 中写入以下内容:

 [webView.scrollView setContentInset:UIEdgeInsetsMake(kNavBarHeight, 0, 0, 0)];
 [webView.scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(kNavBarHeight, 0, 0, 0)];
 [webView.scrollView setContentOffset:CGPointMake(0, -kNavBarHeight) animated:NO];
 webView.scrollView.delegate = self;

then initialize and add your UINavigationBaras a subview to self.viewat origin (Also make sure that your UIWebViewhas the same origin, i.e (0,0)).

然后初始化并将您的UINavigationBar作为子视图添加到self.view原点(还要确保您UIWebView的原点相同,即(0,0))。

3.Implement UIScrollViewDelegatemethods (Don't forget to add UIScrollViewDelegateprotocol):

3.实现UIScrollViewDelegate方法(不要忘记添加UIScrollViewDelegate协议):

#pragma mark - UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == webview.scrollView)
    {
        if (scrollView.contentOffset.y == 1 && !webViewScrollIsDragging && !webViewScrollIsDecelerating)
        {
            [UIView animateWithDuration:0.3
                                  delay:0.0
                                options: UIViewAnimationCurveEaseOut
                             animations:^(void) {
                                 CGRect navBarFrame = CGRectMake(0,-scrollView.contentOffset.y-kNavBarHeight, self.view.bounds.size.width, kNavBarHeight);
                                 navBar.frame = navBarFrame;
                             }
                             completion:nil];
        }
        else
        {
            CGRect navBarFrame = CGRectMake(0,-scrollView.contentOffset.y-kNavBarHeight, self.view.bounds.size.width, kNavBarHeight);
            navBar.frame = navBarFrame;
        }

        if (scrollView.contentOffset.y < -kNavBarHeight)
        {
            [webview.scrollView setScrollIndicatorInsets:UIEdgeInsetsMake(fabsf(scrollView.contentOffset.y), 0, 0, 0)];
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView == webview.scrollView)
    {
        webViewScrollIsDragging = YES;
    }
}

- (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (scrollView == webview.scrollView)
    {
        webViewScrollIsDragging = NO;
    }
}

- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == webview.scrollView)
    {
        webViewScrollIsDecelerating = YES;
    }
}

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == webview.scrollView)
    {
        webViewScrollIsDecelerating = NO;
    }
}

回答by eduludi

Easy as add this on the ViewController implementation file (.m):

很容易在 ViewController 实现文件 (.m) 上添加它:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webView.scrollView.delegate = self;
}  

#pragma mark - UIScrollViewDelegate Methods

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.lastOffsetY = scrollView.contentOffset.y;
}

- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    bool hide = (scrollView.contentOffset.y > self.lastOffsetY);
    [[self navigationController] setNavigationBarHidden:hide animated:YES];

}

AND don't forget to add UIScrollViewDelegateprotocol in the header file (.h) :

并且不要忘记UIScrollViewDelegate在头文件 (.h) 中添加协议:

@interface MyViewController : UIViewController <UIScrollViewDelegate>
    ...
@end

回答by mamian

In iOS8.0 and later just use one line code:

在 iOS8.0 及更高版本中只需使用一行代码:

self.navigationController.hidesBarsOnSwipe = YES;


/// When the user swipes, the navigation controller's navigationBar & toolbar will be hidden (on a swipe up) or shown (on a swipe down). The toolbar only participates if it has items.
//  @property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

When sliding down will hide the navigation bar (and become a transparent state bar), when the slide will be displayed on the navigation bar, the process with animation

向下滑动时会隐藏导航栏(并变成透明状态栏),当滑动时会显示在导航栏上,该过程带有动画