如何从 UITableViewCell 的 UIContextualAction 显示弹出窗口 UIAlertController?
How to display a popover UIAlertController from a UITableViewCell's UIContextualAction?
我想在 UITableViewCell 中通过 UIContextualAction 'button' 呈现 UIAlertController。我的问题是如何为特定的 selected 操作设置 sourceRect?我认为这是可能的,当您在单元格上向左滑动时,iOS 邮件应用程序允许您 select 更多选项(见图)。
设置操作时,它有一个 view
参数。使用它来设置操作 sheet.
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let act1 = UIContextualAction(style: .normal, title: "Bar") { (action, view, completion) in
completion(false)
}
let act2 = UIContextualAction(style: .normal, title: "Foo") { (action, view, completion) in
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Option 1", style: .default) { (action) in
completion(true)
})
alert.addAction(UIAlertAction(title: "Option 2", style: .default) { (action) in
completion(false)
})
// This sets up the alert to show next to the button
alert.popoverPresentationController?.sourceView = view
alert.popoverPresentationController?.sourceRect = view.bounds
self.present(alert, animated: true, completion: nil)
}
return UISwipeActionsConfiguration(actions: [act1, act2])
}
当然,这仅适用于 iPad,因为在 iPhone 上,警报将从屏幕底部显示。
我想在 UITableViewCell 中通过 UIContextualAction 'button' 呈现 UIAlertController。我的问题是如何为特定的 selected 操作设置 sourceRect?我认为这是可能的,当您在单元格上向左滑动时,iOS 邮件应用程序允许您 select 更多选项(见图)。
设置操作时,它有一个 view
参数。使用它来设置操作 sheet.
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let act1 = UIContextualAction(style: .normal, title: "Bar") { (action, view, completion) in
completion(false)
}
let act2 = UIContextualAction(style: .normal, title: "Foo") { (action, view, completion) in
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Option 1", style: .default) { (action) in
completion(true)
})
alert.addAction(UIAlertAction(title: "Option 2", style: .default) { (action) in
completion(false)
})
// This sets up the alert to show next to the button
alert.popoverPresentationController?.sourceView = view
alert.popoverPresentationController?.sourceRect = view.bounds
self.present(alert, animated: true, completion: nil)
}
return UISwipeActionsConfiguration(actions: [act1, act2])
}
当然,这仅适用于 iPad,因为在 iPhone 上,警报将从屏幕底部显示。