简体   繁体   中英

How do I enable horizontal scrolling and disable vertical scrolling in UIWebview?

My UIWebView should not allow vertical scrolling. However, horizontal scrolling should possible.

I have been looking through the documentation, but couldn't find any hints.

The following code disables both scrolling directions, but I want to only disable vertical:

myWebView.scrollView.scrollEnabled = NO;

How do I solve this problem?

Enable Horizontal scrolling and disable Vertical scrolling:

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

the 2nd line there will hide the vertical scroll indicator. Checking if "scrollView.contentOffset.y < zero" will disable the bouncing when you attempt to scroll downwards. You can also do: scrollView.bounces=NO to do the same thing., By just looking at Rama Rao's answer i can tell that his code will reset the scrollView to (0,0) the moment you try to scroll vertically. thereby moving you away from your horizontal position, which is not good.

Enable vertical scrolling and disable Horizontal scrolling:

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

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

PEACE

To make it full work, write:

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

I have just added the last two lines, because when you write it like pnizzle did, you can't scroll from the right to the left side, but still from the left to the right.

Good luck

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

By this way we can solve the issue. Take a global scrollview object as below:

UIScrollView* scrollView =web.scrollView;

set delegate to scroll view then -(void) scrollViewDidScroll:(UIScrollView *)scrollView will be called and you can get the result. I checked it in both versions(iOS 5 & iOS 4.2)

Leave scrollEnabled == YES. The scroll view will restrict scrolling based on the content size. To allow only horizontal scrolling, set the contentSize something like this:

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

The width should be related to the width of whatever is being scrolled inside the scroll view.

Swift 4.2:

Enable Horizontal scrolling and disable Vertical scrolling:

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

Enable vertical scrolling and disable Horizontal scrolling:

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)
    }
}
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 weak var 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM