Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. :(

like image 487
user2966615 Avatar asked Dec 05 '22 07:12

user2966615


1 Answers

Easy as add this on the ViewController implementation file (.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 UIScrollViewDelegate protocol in the header file (.h) :

@interface MyViewController : UIViewController <UIScrollViewDelegate>
    ...
@end
like image 194
eduludi Avatar answered Dec 19 '22 07:12

eduludi