问题
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)
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];
}
回答1:
I have made this in an app, using text area, so i think its the same by replacing textView with webView.
Initialize directions & flags
@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];
}
The scroll
- (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];
}
The tap gesture
- (void)webViewTapped:(id)sender
{
if(!tap){
hideNav = NO;
tap = 1;
} else {
hideNav = YES;
tap = 0;
}
[[self navigationController] setToolbarHidden:hideNav animated:YES];
}
Hope this helps you.
回答2:
The best solution would be to let your Javascript trigger those events and the hosting view controller should handle them.
来源:https://stackoverflow.com/questions/21999839/hide-toolbar-at-bottom-of-uiwebview-when-scrolling-down