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 #1952: Use SafeArea Autolayout Constraints when FormViewController Is Presented Modally #2148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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