11

我的 UIWebView 不应该允许垂直滚动。但是,水平滚动应该是可能的。

我一直在查看文档,但找不到任何提示。

以下代码禁用两个滚动方向,但我只想禁用垂直:

myWebView.scrollView.scrollEnabled = NO;

我该如何解决这个问题?

4

6 回答 6

34

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

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

和平

于 2012-09-25T01:21:42.100 回答
4

要使其充分发挥作用,请编写:

 webview.scrollView.delegate = self;
    [webview.scrollView setShowsHorizontalScrollIndicator:NO];

}

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

我刚刚添加了最后两行,因为当你像 pnizzle 那样编写它时,你不能从右边滚动到左边,但仍然从左边滚动到右边。

祝你好运

于 2014-07-08T19:37:52.697 回答
3
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>web.frame.origin.x) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }
}

通过这种方式,我们可以解决问题。取一个全局滚动视图对象,如下所示:

UIScrollView* scrollView =web.scrollView;

将委托设置为滚动视图然后-(void) scrollViewDidScroll:(UIScrollView *)scrollView将被调用,您可以获得结果。我检查了两个版本(iOS 5 和 iOS 4.2)

于 2012-04-11T04:55:04.277 回答
1

离开scrollEnabled == YES。滚动视图将根据内容大小限制滚动。要仅允许水平滚动,请将 contentSize 设置为:

scrollView.contentSize = CGSizeMake(1000, scrollView.bounds.size.height);

宽度应该与滚动视图中正在滚动的任何内容的宽度相关。

于 2012-04-11T04:56:46.520 回答
1

斯威夫特 4.2:

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

webview.scrollView.delegate = self
webView.scrollView.showsHorizontalScrollIndicator = false

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y > 0  ||  scrollView.contentOffset.y < 0{
        scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y:0)
    }
}

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

webview.scrollView.delegate = self
webView.scrollView.showsHorizontalScrollIndicator = false

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.x > 0 {
        scrollView.contentOffset = CGPoint(x: 0, y:scrollView.contentOffset.y)
    }
}
于 2021-05-05T18:42:45.920 回答
0
i created three seperate UIScrollViews because I wanted three rows, added the IBOutlet below than added the following properties, it gave me no vertical scrolling and horizontal scrolling, i created my own buttons and just added them into the subview for the scroll view i wanted.

@IBOutlet 弱变量 backScrollVIew:UIScrollView!

    @IBOutlet weak var chestScrollVIew: UIScrollView!
    
    @IBOutlet weak var legScrollVIew: UIScrollView!
override func viewDidLoad() {
        super.viewDidLoad()
        
        backScrollVIew.alwaysBounceHorizontal = true
        backScrollVIew.isScrollEnabled = true
        backScrollVIew.contentSize = CGSize(width: 500, height: 100)
        
        chestScrollVIew.isScrollEnabled = true
        chestScrollVIew.isPagingEnabled = false
        chestScrollVIew.contentSize = CGSize(width: 1000, height: 100)
        legScrollVIew.alwaysBounceHorizontal = true
        legScrollVIew.isScrollEnabled = true
        legScrollVIew.contentSize = CGSize(width: 1000, height: 100)
于 2021-08-27T21:14:46.880 回答