Skip to content

Commit 891a96b

Browse files
committed
Added contentInsetAdjustmentBehavior ViewModifier
1 parent 8d26239 commit 891a96b

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

Sources/WebUI/EnhancedWKWebView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ class EnhancedWKWebView: WKWebView {
3737
}
3838
}
3939

40+
var contentInsetAdjustmentBehavior: ContentInsetAdjustmentBehavior = .automatic {
41+
willSet {
42+
self.scrollView.contentInsetAdjustmentBehavior = newValue
43+
}
44+
}
45+
4046
var isRefreshable = false {
4147
willSet {
4248
if newValue {

Sources/WebUI/WebView.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public struct WebView {
2525
private var allowsScrollViewBounces = true
2626
private var allowsOpaqueDrawing = true
2727
private var pageScaleFactor: CGFloat = 1.0
28+
private var contentInsetAdjustmentBehavior = ContentInsetAdjustmentBehavior.automatic
2829
private var isRefreshable = false
2930

3031
/// Creates new WebView.
@@ -120,6 +121,21 @@ public struct WebView {
120121
return modified
121122
}
122123

124+
/// Sets the behavior for determining the adjusted content offsets.
125+
/// - Parameter behavior: The behavior for determining the adjusted content offsets.
126+
/// - Returns: WebView that applies the content inset adgustment behavior.
127+
///
128+
/// This modifier specifies how the safe area insets are used to modify the content area of the scroll view.
129+
/// The default value of this property is `ContentInsetAdjustmentBehavior.automatic`.
130+
///
131+
/// If you want to use a WebView in full screen on iOS and iPadOS,
132+
/// you should specify `ContentInsetAdjustmentBehavior.never` and also apply `SwiftUI.View.ignoresSafeArea()`.
133+
public func contentInsetAdjustmentBehavior(_ behavior: ContentInsetAdjustmentBehavior) -> Self {
134+
var modified = self
135+
modified.contentInsetAdjustmentBehavior = behavior
136+
return modified
137+
}
138+
123139
/// Marks this view as refreshable.
124140
/// - Returns: WebView that reloads page contents when users perform an action to refresh.
125141
///
@@ -143,6 +159,7 @@ public struct WebView {
143159
webView.allowsScrollViewBounces = allowsScrollViewBounces
144160
webView.allowsOpaqueDrawing = allowsOpaqueDrawing
145161
webView.pageScaleFactor = pageScaleFactor
162+
webView.contentInsetAdjustmentBehavior = contentInsetAdjustmentBehavior
146163
webView.isRefreshable = isRefreshable
147164
}
148165

Sources/WebUI/RefreshControl.swift renamed to Sources/WebUI/WorkaroundForPlatformDifferences.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import WebKit
22

3-
/// This is a workaround for the absence of refresh control in macOS.
3+
/// This is a workaround for APIs that are available in UIKit but not in macOS.
44
#if canImport(UIKit)
5+
public typealias ContentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior
56
typealias RefreshControl = UIRefreshControl
67
#else
8+
public enum ContentInsetAdjustmentBehavior {
9+
case automatic
10+
case scrollableAxes
11+
case never
12+
case always
13+
}
14+
715
struct RefreshControl {
816
enum ControlEvent {
917
case valueChanged
@@ -16,6 +24,7 @@ struct RefreshControl {
1624
extension WKWebView {
1725
struct ScrollView {
1826
var bounces = false
27+
var contentInsetAdjustmentBehavior = ContentInsetAdjustmentBehavior.automatic
1928
var refreshControl: RefreshControl? = .init()
2029
}
2130

0 commit comments

Comments
 (0)