Tafel
Tafel

Reputation: 1189

How to subclass UIScrollView of WKWebView in Swift

I successfully subclassed a WKWwebView in Swift. But now I also want to subclass the UIScrollView within that WKWebView. E.g. I'd like to do something like this (pseudo-code):

class CustomWKWebview : WKWebView {
  override init(frame: CGRect, configuration: WKWebViewConfiguration) {
    super.init(frame: frame, configuration: configuration)

    self.scrollView = CustomScrollView // <-- this is what I want
  }
}

Although this seems to be impossible since self.scrollView is declared as read-only by Apple.

Is there another way to subclass the scrollView inside WKWebView anyway?

Upvotes: 0

Views: 496

Answers (1)

Thisura Dodangoda
Thisura Dodangoda

Reputation: 741

The scrollView of WKWebView is a special internal WKScrollView class (which can be confirmed by the following Objective-C Runtime Method).

print(object_getClass(yourWebView.scrollView))

Because of this, any scrollView object you use to replace that of a WKWebView must also inherit from WKScrollView. This is not recommended since it is a private WebKit Framework class to begin with. Using private APIs will cause your App to be rejected from the App Store (speaking from experience).

Looks like you initially wanted to customize another view, by adding HTML/CSS on-top of it. The requirement to subclass the scrollView emerged because you wanted gestures to "pass-through" the view presenting web content.

I suggest the following workarounds.

  1. Try adding the WebView inside of your GMapsView by adding it as a subView of GMapsView - addSubview

  2. Assuming your "GMapsView" is actually "GMSMapView" from the Google Maps SDK for iOS, checkout their Map Styling guide to see if it fits your requirement - Google Maps for iOS SDK Style Reference

Upvotes: 2

Related Questions