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:
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.
setScrollContainer(false); Don't forget to add the webview. setOnTouchListener(...) code above to disable all scrolling in the webview.
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%.
To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With