Skip to content

Commit 766e578

Browse files
committed
feat: improve safe area handling
1 parent 9ecf5ab commit 766e578

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

ios/Extensions.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,38 @@ extension UIView {
3737
}
3838
}
3939

40+
extension UIHostingController {
41+
convenience public init(rootView: Content, ignoreSafeArea: Bool) {
42+
self.init(rootView: rootView)
43+
44+
if ignoreSafeArea {
45+
disableSafeArea()
46+
}
47+
}
48+
49+
/// Disables safe area insets by dynamically subclassing the hosting controller's view
50+
/// and overriding safeAreaInsets to return .zero.
51+
func disableSafeArea() {
52+
guard let viewClass = object_getClass(view) else { return }
53+
54+
let viewSubclassName = String(cString: class_getName(viewClass)).appending("_IgnoreSafeArea")
55+
if let viewSubclass = NSClassFromString(viewSubclassName) {
56+
object_setClass(view, viewSubclass)
57+
}
58+
else {
59+
guard let viewClassNameUtf8 = (viewSubclassName as NSString).utf8String else { return }
60+
guard let viewSubclass = objc_allocateClassPair(viewClass, viewClassNameUtf8, 0) else { return }
61+
62+
if let method = class_getInstanceMethod(UIView.self, #selector(getter: UIView.safeAreaInsets)) {
63+
let safeAreaInsets: @convention(block) (AnyObject) -> UIEdgeInsets = { _ in
64+
return .zero
65+
}
66+
class_addMethod(viewSubclass, #selector(getter: UIView.safeAreaInsets), imp_implementationWithBlock(safeAreaInsets), method_getTypeEncoding(method))
67+
}
68+
69+
objc_registerClassPair(viewSubclass)
70+
object_setClass(view, viewSubclass)
71+
}
72+
}
73+
}
74+

ios/PagerViewProvider.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import SwiftUI
22
import UIKit
33

4-
5-
64
@objc public enum PageScrollState: Int {
75
case idle
86
case dragging
@@ -70,10 +68,6 @@ import UIKit
7068
self.delegate = delegate
7169
}
7270

73-
override public func didUpdateReactSubviews() {
74-
props.children = reactSubviews().map(IdentifiablePlatformView.init)
75-
}
76-
7771
@objc(insertChild:atIndex:)
7872
public func insertChild(_ child: UIView, at index: Int) {
7973
guard index >= 0 && index <= props.children.count else {
@@ -110,13 +104,17 @@ import UIKit
110104
return
111105
}
112106

113-
self.hostingController = UIHostingController(rootView: PagerView(props: props, delegate: delegate))
114-
if let hostingController = self.hostingController, let parentViewController = reactViewController() {
107+
self.hostingController = UIHostingController(
108+
rootView: PagerView(props: props, delegate: delegate),
109+
ignoreSafeArea: true
110+
)
111+
if let hostingController, let parentViewController = reactViewController() {
115112
parentViewController.addChild(hostingController)
116113
hostingController.view.backgroundColor = .clear
117114
addSubview(hostingController.view)
118-
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
119-
hostingController.view.pinEdges(to: self)
115+
hostingController.view.frame = bounds
116+
117+
hostingController.didMove(toParent: parentViewController)
120118
}
121119
}
122120
}

0 commit comments

Comments
 (0)