While attempting to anchor an alert's popoverPresentationController
to a specific UICollectionViewCell
, I discovered it would only anchor to the UINavigationController
.
What steps do I need to take in order to programmatically trigger this alert controller and anchor it to the user-selected cell?
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = collectionView.dequeueReusableCell(withReuseIdentifier: PersonCell.reuseIdentifier, for: indexPath)
let person = people[indexPath.item]
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: { ... }))
alert.addAction(UIAlertAction(title: "Rename", style: .default, handler: { ... }))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
alert.modalPresentationStyle = .popover
alert.popoverPresentationController?.sourceView = selectedCell
alert.popoverPresentationController?.sourceRect = CGRect(x: selectedCell.bounds.maxX, y: selectedCell.bounds.midY, width: 0, height: 0)
present(alert, animated: true)
}