【发布时间】:2019-12-31 05:55:14
【问题描述】:
在尝试将警报的 popoverPresentationController 锚定到特定的 UICollectionViewCell 时,我发现它只会锚定到 UINavigationController。
为了以编程方式触发此警报控制器并将其锚定到用户选择的单元格,我需要采取哪些步骤?
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)
}
【问题讨论】:
-
添加您用来显示警报控制器的代码。
-
@PGDev 添加了我目前正在使用的代码,这似乎与文档一致
-
@Cerlin 是的,但感谢您的链接;我还没有看到那个线程,但它基本上说了我尝试过的同样的事情。我还尝试安全地打开属性并设置在里面。
-
如我提供的链接中所述,如果模态表示样式不是
popover,alert.popoverPresentationController将为 nil。在访问popoverPresentationController之前,先执行alert.modalPresentationStyle = .popover
标签: swift