向下滚动时隐藏UiWebView底部的工具栏

时间:2014-02-24 21:52:31

标签: ios uiwebview

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

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

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

Show toolbar

Hide toolbar

2 个答案:

答案 0 :(得分:2)

我在应用程序中使用文本区域制作了这个,所以我认为通过用webView替换textView来实现它。

初始化路线&标志

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

希望这会对你有所帮助。

答案 1 :(得分:0)

最好的解决方案是让你的Javascript触发这些事件,托管视图控制器应该处理它们。