Skip to content

Commit 3aebe28

Browse files
committed
Update the webview helper to adding offset possibilities
1 parent 69922cf commit 3aebe28

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

TeadsSampleApp/WebViewHelper/TeadsWebViewHelper.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import WebKit
4545

4646
// only webView.scrollView should retain adView as subView
4747
weak var adView: UIView?
48+
weak var containerView: UIView?
4849

4950
private var adViewConstraints = [NSLayoutConstraint]()
5051

@@ -151,26 +152,27 @@ import WebKit
151152
/// - Parameters:
152153
/// - ad: inRead ad
153154
/// - adRatio: ratio of the ad
154-
///
155+
/// - topOffset: topOffset of the View is 0 in most cases, useful if your website display a top bar and don't want the ad to overlap it
156+
/// - bottomOffset: bottomOffset of the View is 0 in most cases, useful if your website display a bottom bar and don't want the ad to overlap it
155157
/// - Note:
156-
/// sould be called from
158+
/// should be called from
157159
/// ```TeadsInReadAdPlacementDelegate.didReceiveAd(ad: TeadsInReadAd, adRatio: TeadsAdRatio)```
158-
@objc public func openSlot(ad: TeadsInReadAd, adRatio: TeadsAdRatio) {
159-
openSlot(adView: TeadsInReadAdView(bind: ad), adRatio: adRatio)
160+
@objc public func openSlot(ad: TeadsInReadAd, adRatio: TeadsAdRatio, topOffset: CGFloat = 0.0, bottomOffset: CGFloat = 0.0) {
161+
openSlot(adView: TeadsInReadAdView(bind: ad), adRatio: adRatio, topOffset: topOffset, bottomOffset: bottomOffset)
160162
}
161163

162-
@objc public func openSlot(adView: UIView, adRatio: TeadsAdRatio = .default) {
164+
@objc public func openSlot(adView: UIView, adRatio: TeadsAdRatio = .default, topOffset: CGFloat = 0.0, bottomOffset: CGFloat = 0.0) {
163165
slotOpener = { [self] in
164166
// update slot with the right ratio
165167
self.updateSlot(adRatio: adRatio)
166168

167169
// in case openSlot is called multiple times
168170
self.adView?.removeFromSuperview()
169171

172+
adView.translatesAutoresizingMaskIntoConstraints = false
173+
self.containerView = createContainerView(topOffset: topOffset, bottomOffset: bottomOffset)
174+
self.containerView?.addSubview(adView)
170175
self.adView = adView
171-
self.webView?.scrollView.addSubview(adView)
172-
self.adView?.translatesAutoresizingMaskIntoConstraints = false
173-
174176
self.evaluateBootstrapInput(JSBootstrapInput.showPlaceholder(0)) { [weak delegate] _, error in
175177
if error != nil {
176178
delegate?.webViewHelperOnError(error: "openSlot failed")
@@ -183,6 +185,22 @@ import WebKit
183185
}
184186
}
185187

188+
private func createContainerView(topOffset: CGFloat, bottomOffset: CGFloat) -> UIView {
189+
let container = UIView()
190+
container.clipsToBounds = true
191+
container.translatesAutoresizingMaskIntoConstraints = false
192+
if let webView = webView {
193+
webView.scrollView.addSubview(container)
194+
NSLayoutConstraint.activate([
195+
container.topAnchor.constraint(equalTo: webView.topAnchor, constant: topOffset),
196+
container.bottomAnchor.constraint(equalTo: webView.bottomAnchor, constant: -bottomOffset),
197+
container.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
198+
container.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
199+
])
200+
}
201+
return container
202+
}
203+
186204
/// Update the slot height with the given ad ratio
187205
/// - Parameters:
188206
/// - adRatio: ratio of the ad

0 commit comments

Comments
 (0)