Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): fix layout issue with new arch #140

Merged
merged 2 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(ios): fix layout issue with new arch
  • Loading branch information
lodev09 committed Feb 23, 2025
commit c4a6e3789feefd1b9844d4ee54cdf693b403c8b4
5 changes: 5 additions & 0 deletions ios/Extensions/UIView+pinTo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ extension UIView {
height: heightConstraint
))
}

func unpin() {
translatesAutoresizingMaskIntoConstraints = true
removeConstraints(constraints)
}
}
62 changes: 26 additions & 36 deletions ios/TrueSheetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,13 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
// MARK: - Content properties

private var containerView: UIView?

private var contentView: UIView?
private var footerView: UIView?
private var scrollView: UIView?

// Reference the bottom constraint to adjust during keyboard event
private var footerViewBottomConstraint: NSLayoutConstraint?

// Reference height constraint during content updates
private var footerViewHeightConstraint: NSLayoutConstraint?

private var scrollableTag: NSNumber?
// Bottom: Reference the bottom constraint to adjust during keyboard event
// Height: Reference height constraint during content updates
private var footerConstraints: Constraints?

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

// Remove all constraints
// Fixes New Arch weird layout degration
containerView?.unpin()
footerView?.unpin()
contentView?.unpin()
scrollView?.unpin()

containerView = nil
contentView = nil
footerView = nil
scrollView = nil
}

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

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

func viewControllerKeyboardWillHide() {
guard let footerViewBottomConstraint else { return }

footerViewBottomConstraint.constant = 0
footerConstraints?.bottom?.constant = 0

UIView.animate(withDuration: 0.3) {
self.viewController.view.layoutIfNeeded()
}
}

func viewControllerKeyboardWillShow(_ keyboardHeight: CGFloat) {
guard let footerViewBottomConstraint else { return }

footerViewBottomConstraint.constant = -keyboardHeight
footerConstraints?.bottom?.constant = -keyboardHeight

UIView.animate(withDuration: 0.3) {
self.viewController.view.layoutIfNeeded()
Expand Down Expand Up @@ -193,7 +192,13 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
}

func viewControllerWillAppear() {
setupScrollable()
guard let contentView, let scrollView, let containerView else {
return
}

// Add constraints to fix weirdness and support ScrollView
contentView.pinTo(view: containerView, constraints: nil)
scrollView.pinTo(view: contentView, constraints: nil)
}

func viewControllerDidDismiss() {
Expand Down Expand Up @@ -257,19 +262,18 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
@objc
func setFooterHeight(_ height: NSNumber) {
let footerHeight = CGFloat(height.floatValue)
guard let footerView, let footerViewHeightConstraint,
viewController.footerHeight != footerHeight else {
guard let footerView, viewController.footerHeight != footerHeight else {
return
}

viewController.footerHeight = footerHeight

if footerView.subviews.first != nil {
containerView?.bringSubviewToFront(footerView)
footerViewHeightConstraint.constant = viewController.footerHeight
footerConstraints?.height?.constant = viewController.footerHeight
} else {
containerView?.sendSubviewToBack(footerView)
footerViewHeightConstraint.constant = 0
footerConstraints?.height?.constant = 0
}

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

@objc
func setScrollableHandle(_ tag: NSNumber?) {
scrollableTag = tag
scrollView = uiManager?.view(forReactTag: tag)
}

// MARK: - Methods
Expand All @@ -391,20 +395,6 @@ class TrueSheetView: UIView, RCTInvalidating, TrueSheetViewControllerDelegate {
}
}

func setupScrollable() {
guard let contentView, let containerView, let scrollableTag else {
return
}

let scrollView = uiManager?.view(forReactTag: scrollableTag)

// Add constraints to fix weirdness and support ScrollView
if let scrollView {
contentView.pinTo(view: containerView, constraints: nil)
scrollView.pinTo(view: contentView, constraints: nil)
}
}

func dispatchEvent(name: String, data: [String: Any]?) {
eventDispatcher?.send(TrueSheetEvent(viewTag: reactTag, name: name, data: data))
}
Expand Down
Loading