user2966615 user2966615 - 3 months ago 33
Objective-C Question

hide and show navbar when scroll uiwebview

i need help, i am trying to build a app in which i have a

viewcontroller
with a
uiwebview
and a
navbar
with 2 buttons on it. what i want to do is that when user scroll
uiwebview
navbar automatically hides like slid up sort of. but is not working they way i want it to work. let me post code here.
In
viewdidload
i put this.

[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
NSLog
correctly but nothing else
navbar
still stays. :(

Answer

You can try this:

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

UINavigationBar *navBar;

static const CGFloat kNavBarHeight = 60.0f;

BOOL webViewScrollIsDragging;
BOOL webViewScrollIsDecelerating;

2. In viewDidLoad write the following:

 [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 UINavigationBar as a subview to self.view at origin (Also make sure that your UIWebView has the same origin, i.e (0,0)).

3. Implement UIScrollViewDelegate methods (Don't forget to add UIScrollViewDelegate protocol):

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