如何在iOS中的UIWebView中进行水平滚动?

时间:2016-03-22 05:48:51

标签: ios objective-c uiscrollview uiwebview

我正在UIWebView加载一个pptx文件,它正在加载并且它是垂直滚动的,但它应该是水平滚动的。这是我的代码。

readWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height)];
     NSString *path=[[NSBundle mainBundle] pathForResource:@"MyPPT" ofType:@"pptx"];
      readWebView.scrollView.delegate=self;
        NSURL *url=[NSURL fileURLWithPath:path];
        NSURLRequest *request=[NSURLRequest requestWithURL:url];
        readWebView.scalesPageToFit=YES;
        readWebView.scrollView.showsVerticalScrollIndicator=false;
        readWebView.scrollView.showsHorizontalScrollIndicator=true;
        readWebView.scrollView.scrollEnabled=YES;
        readWebView.autoresizingMask=UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        readWebView.scrollView.pagingEnabled=YES;
     [readWebView loadRequest:request];
    [self.view addSubview:readWebView];

2 个答案:

答案 0 :(得分:1)

设置为

readWebView.scrollView.delegate = self;
[readWebView.scrollView setShowsVerticalScrollIndicator:NO];


-(void)scrollViewDidScroll:(UIScrollView *)scrollVie
{
if (scrollVie.contentOffset.y > 0  ||  scrollVie.contentOffset.y < 0 )
    scrollVie.contentOffset = CGPointMake(scrollVie.contentOffset.x, 0);
}

答案 1 :(得分:0)

启用水平滚动并禁用垂直滚动:

myWebView.scrollView.delegate = self;
[myWebView.scrollView setShowsVerticalScrollIndicator:NO];

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y > 0  ||  scrollView.contentOffset.y < 0 )
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, 0);
}

第二行会隐藏垂直滚动指示。检查“scrollView.contentOffset.y&lt; zero”是否会在您尝试向下滚动时禁用弹跳。你也可以这样做:scrollView.bounces = NO做同样的事!!通过查看Rama Rao的答案,我可以看出他的代码会在您尝试垂直滚动时将scrollView重置为(0.0),从而使您远离水平位置,这是不好的。

启用垂直滚动并禁用水平滚动:

myWebView.scrollView.delegate = self;
[myWebview.scrollView setShowsHorizontalScrollIndicator:NO];

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x > 0)
        scrollView.contentOffset = CGPointMake(0, scrollView.contentOffset.y);
}