滚动uiwebview时隐藏并显示导航栏

时间:2014-01-11 07:14:20

标签: iphone objective-c xcode navbar

我需要帮助,我正在尝试构建一个应用,其中viewcontroller的{​​{1}}和uiwebview上有2个按钮。我想要做的是,当用户滚动navbar导航栏自动隐藏时,就像滑倒了一样。但是他们不按他希望的方式工作。让我在这里发布代码。 在uiwebview我把它。

viewdidload

然后我有这个方法

[webPage.scrollView setDelegate:self];

- (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 } } 正确但没有其他NSLog仍然存在。 :(

3 个答案:

答案 0 :(得分:11)

轻松将其添加到ViewController实现文件(.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];

}

并且不要忘记在头文件(.h)中添加UIScrollViewDelegate协议:

@interface MyViewController : UIViewController <UIScrollViewDelegate>
    ...
@end

答案 1 :(得分:4)

你可以试试这个:

1。声明导航栏,导航栏高度和2个BOOL变量的常量:

UINavigationBar *navBar;

static const CGFloat kNavBarHeight = 60.0f;

BOOL webViewScrollIsDragging;
BOOL webViewScrollIsDecelerating;

2. 在viewDidLoad中写下以下内容:

 [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;

然后初始化并将UINavigationBar作为子视图添加到self.view的原点(同时确保您的UIWebView具有相同的来源,即(0,0))。

3。实施UIScrollViewDelegate方法(不要忘记添加UIScrollViewDelegate协议):

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

答案 2 :(得分:1)

在iOS8.0及更高版本中 只需使用一行代码:

<SeekBar
    android:id="@+id/progress_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/progress_bar_height"
    android:minHeight="@dimen/progress_bar_height"
    android:maxHeight="@dimen/progress_bar_height"
    android:progressDrawable="@drawable/progressbar_seek_bar"
    android:thumbOffset="0dp"
    android:thumb="@drawable/progressbar_thumb"
    android:max="100"/>

当向下滑动时会隐藏导航栏(并成为透明状态栏),当幻灯片将显示在导航栏上时,带有动画的过程