Skip to content

Commit c4a6e37

Browse files
committed
fix(ios): fix layout issue with new arch
1 parent 2919972 commit c4a6e37

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

ios/Extensions/UIView+pinTo.swift

+5
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ extension UIView {
6666
height: heightConstraint
6767
))
6868
}
69+
70+
func unpin() {
71+
translatesAutoresizingMaskIntoConstraints = true
72+
removeConstraints(constraints)
73+
}
6974
}

ios/TrueSheetView.swift

+26-36
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,13 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
4141
// MARK: - Content properties
4242

4343
private var containerView: UIView?
44-
4544
private var contentView: UIView?
4645
private var footerView: UIView?
46+
private var scrollView: UIView?
4747

48-
// Reference the bottom constraint to adjust during keyboard event
49-
private var footerViewBottomConstraint: NSLayoutConstraint?
50-
51-
// Reference height constraint during content updates
52-
private var footerViewHeightConstraint: NSLayoutConstraint?
53-
54-
private var scrollableTag: NSNumber?
48+
// Bottom: Reference the bottom constraint to adjust during keyboard event
49+
// Height: Reference height constraint during content updates
50+
private var footerConstraints: Constraints?
5551

5652
private var uiManager: RCTUIManager? {
5753
guard let uiManager = bridge?.uiManager else { return nil }
@@ -104,9 +100,17 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
104100
touchHandler.detach(from: subview)
105101
surfaceTouchHandler.detach(from: subview)
106102

103+
// Remove all constraints
104+
// Fixes New Arch weird layout degration
105+
containerView?.unpin()
106+
footerView?.unpin()
107+
contentView?.unpin()
108+
scrollView?.unpin()
109+
107110
containerView = nil
108111
contentView = nil
109112
footerView = nil
113+
scrollView = nil
110114
}
111115

112116
override func didUpdateReactSubviews() {
@@ -131,8 +135,7 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
131135
// Set footer constraints
132136
if let footerView {
133137
footerView.pinTo(view: viewController.view, from: [.left, .right, .bottom], with: 0) { constraints in
134-
self.footerViewBottomConstraint = constraints.bottom
135-
self.footerViewHeightConstraint = constraints.height
138+
self.footerConstraints = constraints
136139
}
137140

138141
// Set initial footer height
@@ -153,19 +156,15 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
153156
// MARK: - ViewController delegate
154157

155158
func viewControllerKeyboardWillHide() {
156-
guard let footerViewBottomConstraint else { return }
157-
158-
footerViewBottomConstraint.constant = 0
159+
footerConstraints?.bottom?.constant = 0
159160

160161
UIView.animate(withDuration: 0.3) {
161162
self.viewController.view.layoutIfNeeded()
162163
}
163164
}
164165

165166
func viewControllerKeyboardWillShow(_ keyboardHeight: CGFloat) {
166-
guard let footerViewBottomConstraint else { return }
167-
168-
footerViewBottomConstraint.constant = -keyboardHeight
167+
footerConstraints?.bottom?.constant = -keyboardHeight
169168

170169
UIView.animate(withDuration: 0.3) {
171170
self.viewController.view.layoutIfNeeded()
@@ -193,7 +192,13 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
193192
}
194193

195194
func viewControllerWillAppear() {
196-
setupScrollable()
195+
guard let contentView, let scrollView, let containerView else {
196+
return
197+
}
198+
199+
// Add constraints to fix weirdness and support ScrollView
200+
contentView.pinTo(view: containerView, constraints: nil)
201+
scrollView.pinTo(view: contentView, constraints: nil)
197202
}
198203

199204
func viewControllerDidDismiss() {
@@ -257,19 +262,18 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
257262
@objc
258263
func setFooterHeight(_ height: NSNumber) {
259264
let footerHeight = CGFloat(height.floatValue)
260-
guard let footerView, let footerViewHeightConstraint,
261-
viewController.footerHeight != footerHeight else {
265+
guard let footerView, viewController.footerHeight != footerHeight else {
262266
return
263267
}
264268

265269
viewController.footerHeight = footerHeight
266270

267271
if footerView.subviews.first != nil {
268272
containerView?.bringSubviewToFront(footerView)
269-
footerViewHeightConstraint.constant = viewController.footerHeight
273+
footerConstraints?.height?.constant = viewController.footerHeight
270274
} else {
271275
containerView?.sendSubviewToBack(footerView)
272-
footerViewHeightConstraint.constant = 0
276+
footerConstraints?.height?.constant = 0
273277
}
274278

275279
if #available(iOS 15.0, *) {
@@ -366,7 +370,7 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
366370

367371
@objc
368372
func setScrollableHandle(_ tag: NSNumber?) {
369-
scrollableTag = tag
373+
scrollView = uiManager?.view(forReactTag: tag)
370374
}
371375

372376
// MARK: - Methods
@@ -391,20 +395,6 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
391395
}
392396
}
393397

394-
func setupScrollable() {
395-
guard let contentView, let containerView, let scrollableTag else {
396-
return
397-
}
398-
399-
let scrollView = uiManager?.view(forReactTag: scrollableTag)
400-
401-
// Add constraints to fix weirdness and support ScrollView
402-
if let scrollView {
403-
contentView.pinTo(view: containerView, constraints: nil)
404-
scrollView.pinTo(view: contentView, constraints: nil)
405-
}
406-
}
407-
408398
func dispatchEvent(name: String, data: [String: Any]?) {
409399
eventDispatcher?.send(TrueSheetEvent(viewTag: reactTag, name: name, data: data))
410400
}

0 commit comments

Comments
 (0)