Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable horizontal scrolling in webView with swift?

I have a simple webView like :

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()

    let url = "https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-XID_456"
    let requestURL = NSURL(string:url)
    let request = NSURLRequest(URL: requestURL!)
    webView.loadRequest(request)

when I load this request into webView it loads perfectly fine but when Its load completely It scroll horizontally like this when I scroll it from left side and right side:

enter image description here

enter image description here

I don't want to scroll it this way and I only want it to scroll vertically is there any way to do it in swift I search on Internet but only found solution for objective-c and I try this in swift but not working.

Please provide me any solution for this.

like image 241
Dharmesh Kheni Avatar asked Dec 17 '14 05:12

Dharmesh Kheni


People also ask

How do I disable scrolling in Webview?

setScrollContainer(false); Don't forget to add the webview. setOnTouchListener(...) code above to disable all scrolling in the webview.

How do I stop horizontal scrolling in web design?

You can also set the overflow-x CSS property to hidden, which prevents child content from wrapping within its container but turns off sideways scrolling. Another solution is to set the width of child elements to 100%.

How do I turn off horizontal scrolling?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.


1 Answers

Update Swift 4

Add both these methods into your UIViewController:

    func webViewDidFinishLoad(_ webView: UIWebView) {
    self.webView.scrollView.showsHorizontalScrollIndicator = false
}

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

in the viewDidLoad Method add these lines:

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

Now this wld show an error at line webview.scrollView.delegate = self so make sure you add the UIScrollViewDelegate like this

class DetailViewController: UIViewController, UIWebViewDelegate, UIScrollViewDelegate{

And horizontal scrolling has stopped :)

like image 161
Swapna Lekshmanan Avatar answered Sep 21 '22 15:09

Swapna Lekshmanan