@@ -122,21 +122,13 @@ class IterableInAppHTMLViewController: UIViewController {
122
122
}
123
123
124
124
override var prefersStatusBarHidden : Bool { return true }
125
-
126
- override func viewWillLayoutSubviews( ) {
127
- super. viewWillLayoutSubviews ( )
128
- if let webView = webView {
129
- resizeWebView ( webView)
130
- }
131
- }
132
125
133
126
private let htmlString : String
134
127
private var insetPadding : UIEdgeInsets = UIEdgeInsets . zero
135
128
private var customBlockCallback : ITBURLCallback ?
136
129
private var trackParams : IterableNotificationMetadata ?
137
130
private var webView : WKWebView ?
138
131
private var location : InAppNotificationType = . full
139
- private var loaded = false
140
132
141
133
required init ? ( coder aDecoder: NSCoder ) {
142
134
self . htmlString = aDecoder. decodeObject ( forKey: " htmlString " ) as? String ?? " "
@@ -150,47 +142,62 @@ class IterableInAppHTMLViewController: UIViewController {
150
142
- parameter: aWebView the webview
151
143
*/
152
144
private func resizeWebView( _ aWebView: WKWebView ) {
153
- guard loaded else {
154
- return
155
- }
156
145
guard location != . full else {
157
146
webView? . frame = CGRect ( x: 0 , y: 0 , width: view. frame. width, height: view. frame. height)
158
147
return
159
148
}
160
149
161
- // Resizes the frame to match the HTML content with a max of the screen size.
162
- var frame = aWebView. frame
163
- frame. size. height = 1
164
- aWebView. frame = frame;
165
- let fittingSize = aWebView. scrollView. contentSize
166
- frame. size = fittingSize
150
+ aWebView. evaluateJavaScript ( " document.body.offsetHeight " , completionHandler: { height, _ in
151
+ guard let floatHeight = height as? CGFloat , floatHeight >= 20 else {
152
+ ITBError ( " unable to get height " )
153
+ return
154
+ }
155
+ self . resize ( webView: aWebView, withHeight: floatHeight)
156
+ } )
157
+ }
158
+
159
+ private func resize( webView: WKWebView , withHeight height: CGFloat ) {
160
+ ITBInfo ( " height: \( height) " )
161
+ // set the height
162
+ webView. frame. size. height = height
163
+
164
+ // now set the width
167
165
let notificationWidth = 100 - ( insetPadding. left + insetPadding. right)
168
166
let screenWidth = view. bounds. width
169
- frame. size. width = screenWidth*notificationWidth/ 100
170
- frame. size. height = min ( frame. height, self . view. bounds. height)
171
- aWebView. frame = frame;
167
+ webView. frame. size. width = screenWidth * notificationWidth / 100
172
168
173
- let resizeCenterX = screenWidth* ( insetPadding. left + notificationWidth/ 2 ) / 100
174
-
175
169
// Position webview
176
- var center = self . view. center
177
- let webViewHeight = aWebView. frame. height/ 2
170
+ var center = view. center
171
+
172
+ // set center x
173
+ center. x = screenWidth * ( insetPadding. left + notificationWidth / 2 ) / 100
174
+
175
+ // set center y
176
+ let halfWebViewHeight = webView. frame. height / 2
178
177
switch location {
179
178
case . top:
180
- center. y = webViewHeight
179
+ if #available( iOS 11 , * ) {
180
+ center. y = halfWebViewHeight + view. safeAreaInsets. top
181
+ } else {
182
+ center. y = halfWebViewHeight
183
+ }
181
184
case . bottom:
182
- center. y = view. frame. height - webViewHeight
183
- case . center, . full: break
185
+ if #available( iOS 11 , * ) {
186
+ center. y = view. frame. height - halfWebViewHeight - view. safeAreaInsets. bottom
187
+ } else {
188
+ center. y = view. frame. height - halfWebViewHeight
189
+ }
190
+ default : break
184
191
}
185
- center . x = resizeCenterX;
186
- aWebView . center = center;
192
+
193
+ webView . center = center
187
194
}
188
195
}
189
196
190
197
extension IterableInAppHTMLViewController : WKNavigationDelegate {
191
198
func webView( _ webView: WKWebView , didFinish navigation: WKNavigation ! ) {
192
199
ITBInfo ( )
193
- loaded = true
200
+
194
201
if let myWebview = self . webView {
195
202
resizeWebView ( myWebview)
196
203
}
0 commit comments