Skip to content

Commit

Permalink
Fix #1952: Use SafeArea Autolayout Constraints when FormViewControlle…
Browse files Browse the repository at this point in the history
…r Is Presented Modally
  • Loading branch information
philipbel committed Mar 29, 2021
1 parent 523af88 commit 6ac7f35
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Source/Core/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,22 @@ open class FormViewController: UIViewController, FormViewControllerProtocol, For
navigationAccessoryView = customNavigationAccessoryView ?? NavigationAccessoryView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44.0))
navigationAccessoryView.autoresizingMask = .flexibleWidth

var shouldSetupConstraints = false
if tableView == nil {
tableView = UITableView(frame: view.bounds, style: tableViewStyle)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.cellLayoutMarginsFollowReadableWidth = false
shouldSetupConstraints = true
}
if tableView.superview == nil {
view.addSubview(tableView)
}
if shouldSetupConstraints {
setupTableViewConstraintsIfPresented()
}
// Always set up a mask. If the UITableView uses constraints, `translatesAutoresizingMaskIntoConstraints` will
// be set to false, and so the autoresizing mask will be ignored.
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

if tableView.delegate == nil {
tableView.delegate = self
}
Expand Down Expand Up @@ -739,6 +747,28 @@ open class FormViewController: UIViewController, FormViewControllerProtocol, For
tableView?.endUpdates()
}

/**
* Fix for #1952: `UITableView.scrollRectToVisible(, animated:)` and `UITableView.scrollToRow()` don't work properly
* in `keyboardWillShow()` due to, what seems to be, a bug in iOS when autoresizing mask is used for a `UIViewController` that is
* presented modally as `.formSheet/.pageSheet` within a `UINavigationController`.
* The check below preserves the existing behavior of using autoresizing masks in other cases.
*/
open func setupTableViewConstraintsIfPresented() {
if #available(iOS 13, *) {
if presentingViewController != nil && navigationController != nil {
// Need to set the background color as otherwise black shows underneath the translucent navigation bar.
view.backgroundColor = .systemBackground
tableView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.topAnchor),
tableView.bottomAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.bottomAnchor),
tableView.leadingAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.leadingAnchor),
tableView.trailingAnchor.constraint(equalTo: tableView.superview!.safeAreaLayoutGuide.trailingAnchor)
])
}
}
}

// MARK: Private

var oldBottomInset: CGFloat?
Expand Down

0 comments on commit 6ac7f35

Please sign in to comment.