Skip to content

Commit 6ac7f35

Browse files
committed
Fix #1952: Use SafeArea Autolayout Constraints when FormViewController Is Presented Modally
1 parent 523af88 commit 6ac7f35

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Source/Core/Core.swift

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,22 @@ open class FormViewController: UIViewController, FormViewControllerProtocol, For
456456
navigationAccessoryView = customNavigationAccessoryView ?? NavigationAccessoryView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44.0))
457457
navigationAccessoryView.autoresizingMask = .flexibleWidth
458458

459+
var shouldSetupConstraints = false
459460
if tableView == nil {
460461
tableView = UITableView(frame: view.bounds, style: tableViewStyle)
461-
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
462462
tableView.cellLayoutMarginsFollowReadableWidth = false
463+
shouldSetupConstraints = true
463464
}
464465
if tableView.superview == nil {
465466
view.addSubview(tableView)
466467
}
468+
if shouldSetupConstraints {
469+
setupTableViewConstraintsIfPresented()
470+
}
471+
// Always set up a mask. If the UITableView uses constraints, `translatesAutoresizingMaskIntoConstraints` will
472+
// be set to false, and so the autoresizing mask will be ignored.
473+
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
474+
467475
if tableView.delegate == nil {
468476
tableView.delegate = self
469477
}
@@ -739,6 +747,28 @@ open class FormViewController: UIViewController, FormViewControllerProtocol, For
739747
tableView?.endUpdates()
740748
}
741749

750+
/**
751+
* Fix for #1952: `UITableView.scrollRectToVisible(, animated:)` and `UITableView.scrollToRow()` don't work properly
752+
* in `keyboardWillShow()` due to, what seems to be, a bug in iOS when autoresizing mask is used for a `UIViewController` that is
753+
* presented modally as `.formSheet/.pageSheet` within a `UINavigationController`.
754+
* The check below preserves the existing behavior of using autoresizing masks in other cases.
755+
*/
756+
open func setupTableViewConstraintsIfPresented() {
757+
if #available(iOS 13, *) {
758+
if presentingViewController != nil && navigationController != nil {
759+
// Need to set the background color as otherwise black shows underneath the translucent navigation bar.
760+
view.backgroundColor = .systemBackground
761+
tableView.translatesAutoresizingMaskIntoConstraints = false
762+
NSLayoutConstraint.activate([
763+
tableView.topAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.topAnchor),
764+
tableView.bottomAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.bottomAnchor),
765+
tableView.leadingAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.leadingAnchor),
766+
tableView.trailingAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.trailingAnchor)
767+
])
768+
}
769+
}
770+
}
771+
742772
// MARK: Private
743773

744774
var oldBottomInset: CGFloat?

0 commit comments

Comments
 (0)