Как отобразить всплывающий UIAlertController из UIContextualAction UITableViewCell? - PullRequest
0 голосов
/ 22 октября 2018

Я хотел бы представить UIAlertController через «кнопку» UIContextualAction в UITableViewCell.Мой вопрос, как установить sourceRect для конкретного выбранного действия?Я предполагаю, что это возможно, так как приложение Почта iOS позволяет вам выбирать больше параметров, когда вы проводите влево по ячейке (см. Изображение).enter image description here

1 Ответ

0 голосов
/ 22 октября 2018

Когда вы настраиваете действие, оно имеет параметр view.Используйте это для настройки листа действий.

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 предупреждение будет отображаться в нижней части экрана.

...