Skip to content

Commit

Permalink
Fix a crash in Samples app on iPad (#497)
Browse files Browse the repository at this point in the history
* Fix a crash when the app shows an action sheet on iPad
* Use the UITableViewCell as the sourceView
  • Loading branch information
milettal authored Sep 25, 2021
1 parent 5f1d49b commit 31d7a2a
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ class DebugTableViewController: InspectableViewController {
}
}

func execute(for vc: DebugTableViewController) {
func execute(for vc: DebugTableViewController, sourceView: UIView) {
switch self {
case .animateScroll:
vc.animateScroll()
case .changeContentSize:
vc.changeContentSize()
vc.changeContentSize(sourceView: sourceView)
case .moveToFull:
vc.moveToFull()
case .moveToHalf:
Expand Down Expand Up @@ -165,8 +165,8 @@ class DebugTableViewController: InspectableViewController {

// MARK: - Actions

private func execute(command: Command) {
command.execute(for: self)
private func execute(command: Command, sourceView: UIView) {
command.execute(for: self, sourceView: sourceView)
}

@objc
Expand All @@ -177,7 +177,7 @@ class DebugTableViewController: InspectableViewController {
}

@objc
private func changeContentSize() {
private func changeContentSize(sourceView: UIView) {
let actionSheet = UIAlertController(title: "Change content size", message: "", preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "Large", style: .default, handler: { (_) in
self.itemHeight = 66.0
Expand All @@ -201,6 +201,11 @@ class DebugTableViewController: InspectableViewController {
self.changeItems(3)
}))

if let popoverController = actionSheet.popoverPresentationController {
popoverController.sourceView = sourceView
popoverController.sourceRect = sourceView.bounds
}

self.present(actionSheet, animated: true, completion: nil)
}

Expand Down Expand Up @@ -250,7 +255,8 @@ extension DebugTableViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("DebugTableViewController -- select row \(indexPath.row)")
guard let action = Command(rawValue: indexPath.row) else { return }
execute(command: action)
let cell = tableView.cellForRow(at: indexPath)
execute(command: action, sourceView: cell ?? tableView)
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
Expand Down

0 comments on commit 31d7a2a

Please sign in to comment.