向下滚动时隐藏 UiWebView 底部的工具栏 [英] hide toolbar at bottom of UiWebView when scrolling down

查看:22
本文介绍了向下滚动时隐藏 UiWebView 底部的工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,当不在 Web 视图顶部时成功隐藏底部工具栏,如附图所示.(下)

I have the following code that successfully hides the bottom toolbar when NOT at the top of the web view as shown in attached images. (below)

我想要做的是完全隐藏工具栏,然后展开 web 视图以占用额外的空间(类似于 Safari 的做法).任何帮助都会很棒!

What I am trying to do is hide the toolbar completely and then expand the web view to take up the extra space (similar to how Safari does this). Any help would be great!

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (self.url != nil)
    {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[StoreWebViewController checkAndPrependProtocolForUrl:self.url]]]];
        self.webView.delegate = self;
    }

    for (id subview in self.webView.subviews)
    {
        if ([[subview class] isSubclassOfClass: [UIScrollView class]])
        {
            UIScrollView * s = (UIScrollView*)subview;
            originalDelegate = s.delegate;
            s.delegate = self;
            break;
        }
    }
}


- (void)scrollViewDidScroll :(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y == 0) //Show toolbar when at top
    {
        [self.toolbar setHidden:NO];
    }
    else
    {
        [self.toolbar setHidden:YES];
    }
    [originalDelegate scrollViewDidScroll: scrollView];
}

推荐答案

我在一个应用程序中做了这个,使用文本区域,所以我认为用 webView 替换 textView 是一样的.

I have made this in an app, using text area, so i think its the same by replacing textView with webView.

初始化路线&标志

@implementation YourViewController{
    BOOL tap;
    BOOL hideNav;
    BOOL mustShowNav;
}

@synthesize webView;

typedef enum ScrollDirection {
    ScrollDirectionNone,
    ScrollDirectionRight,
    ScrollDirectionLeft,
    ScrollDirectionUp,
    ScrollDirectionDown,
} ScrollDirection;

viewDidLoad

- (void)viewDidLoad 
{
    webView.delegate = self;
    hideNav = NO;
    mustShowNav = NO;

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(webViewTapped:)];
    gestureRecognizer.delegate = self;
    [self.webView addGestureRecognizer:gestureRecognizer];
}

滚动

- (void)scrollViewDidScroll:(UIScrollView*)scrollView
{
    ScrollDirection scrollDirection;

    if (lastContentOffset.y < scrollView.contentOffset.y)
        scrollDirection = ScrollDirectionDown;
    else
        scrollDirection = ScrollDirectionUp;


    float endScrolling = scrollView.contentOffset.y + scrollView.frame.size.height;
    if (scrollDirection == ScrollDirectionDown && scrollView.contentOffset.y > 50 && !mustShowNav) {
        hideNav = YES;
        tap = 0;
    } else {
        hideNav = NO;
    }

    if (scrollDirection == ScrollDirectionUp && mustShowNav){
        hideNav = NO;
        mustShowNav = NO;
    }

    if (scrollDirection == ScrollDirectionDown && endScrolling > scrollView.contentSize.height - 50 && !mustShowNav) {
        mustShowNav = YES;
    }

    [[self navigationController] setToolbarHidden:hideNav animated:YES];
    lastContentOffset = scrollView.contentOffset;

    [originalDelegate scrollViewDidScroll: scrollView];
}

点击手势

- (void)webViewTapped:(id)sender
{
    if(!tap){
        hideNav = NO;
        tap = 1;
    } else {
        hideNav = YES;
        tap = 0;
    }
    [[self navigationController] setToolbarHidden:hideNav animated:YES];
}

希望对你有帮助.

这篇关于向下滚动时隐藏 UiWebView 底部的工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆