비전공자 개발일기

WKWebView OverScroll Disable Setting 본문

SWIFT

WKWebView OverScroll Disable Setting

HiroDaegu 2023. 3. 15. 15:21
728x90
SMALL

<1안>

var isScrolling = false
...

let preferences = WKPreferences()
preferences.javaScriptCanOpenWindowsAutomatically = true

...

let contentController = WKUserContentController()

...

let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
configuration.userContentController = contentController

...

webView = WKWebView(frame: self.view.bounds, configuration: configuration)

...

webView.scrollView.delegate = self

...

webView.scrollView.alwaysBounceVertical = false
webView.scrollView.bounces = false

...

extension ViewController: UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard !isScrolling else { return }
        isScrolling = true
        
        if scrollView.contentOffset.y <= 0 {
            scrollView.contentOffset = CGPoint.zero
        }
        if scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.bounds.size.height {
            let offset = CGPoint(x: scrollView.contentOffset.x, y: scrollView.contentSize.height - scrollView.bounds.size.height)
            scrollView.setContentOffset(offset, animated: false)
        }
        
        isScrolling = false
    }
    
}

 

<2안>

...

let preferences = WKPreferences()
preferences.javaScriptCanOpenWindowsAutomatically = true

...

let contentController = WKUserContentController()

...

let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
configuration.userContentController = contentController

...

webView = WKWebView(frame: self.view.bounds, configuration: configuration)

...

webView.scrollView.delegate = self

...

webView.scrollView.alwaysBounceVertical = false
webView.scrollView.bounces = false
if #available(iOS 11.0, *) {
    webView.scrollView.contentInsetAdjustmentBehavior = .never
} else {
    automaticallyAdjustsScrollViewInsets = false
}

...
728x90
LIST

'SWIFT' 카테고리의 다른 글

Xcode Simulator Cache Delete  (0) 2023.04.21
Virate Method  (0) 2023.04.14
Swift WKWebView Debugging with Safari  (0) 2023.03.09
WKWebView URL Query & Header Setting  (0) 2023.03.07
SafeArea Color Control  (0) 2023.03.06