ios - 如何在 Swift 中为 View 集合中的每个单元格添加弹出窗口

标签 ios swift uicollectionview uipopovercontroller uipopoverpresentationcontroller

我正在用文字编写一个应用程序(一种游戏)。所以,我有带有 CollectionView 的 WordsVC(其中每个单元格都是一个词)。当长按单词(单元格)时,我想在单元格旁边显示带有翻译的弹出窗口。

但我无法将 segue 添加到单元格(Xcode 给我一个错误,类似于“无法编译”)。 所以,我正在从 CollectionView 转到 TraslationVC(popover)。这就是问题所在,因为弹出窗口会在 View 集合的左上角弹出(我需要在点击的单元格旁边)。

我无法通过搜索很好地回答。我该怎么做才能实现它?

这是一些代码:

在 WordsVC 中为 segue 做准备:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // for another segue
    if segue.identifier == "fromWordsToWin" {
        if let wvc = sender as? WordsViewController {
            if let winVC = segue.destination as? WinningViewController {
                winVC.completedLevel = wvc.currentLevel
                winVC.levelsCount = wvc.dataSource.count()
                winVC.resultTime = wvc.result
            }
        }
    }
    // here
    if segue.identifier == "translationSegue" {
        if let cell = sender as? WordCell {
            if let tvc = segue.destination as? TranslationViewController {
                tvc.text = cell.myLabel.text ?? "empty cell"
                if let ppc = tvc.popoverPresentationController {
                    ppc.delegate = self
                }

            }
        }
    }
}

在 WordsVC 中设置非模态样式:

 func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return UIModalPresentationStyle.none
}

Segueing(来自 WordsVC):

@objc func longTap(_ sender: UIGestureRecognizer) {
    print("long tap happend")
    if let cell = sender.view as? WordCell {
        performSegue(withIdentifier: "translationSegue", sender: cell)
    }

并在 TranslatingVC 中设置 popover 的大小:

 override var preferredContentSize: CGSize {
    get {
        if textView != nil && presentingViewController != nil {
            return textView.sizeThatFits(presentingViewController!.view.bounds.size)
        } else {
            return super.preferredContentSize
        }
    }
    set { super.preferredContentSize = newValue}
}

如何做到这一点?

最佳答案

有人建议我使用 popoverViewController.sourceView。所以,它运作良好!我还添加了一些关于弹出窗口确切位置的设置。我在下面的 prepareForSegue 中的代码(代码中的最后两行):

 if segue.identifier == "translationSegue" {
        if let cell = sender as? WordCell {
            if let tvc = segue.destination as? TranslationViewController {
                tvc.text = cell.myLabel.text ?? "empty cell"
                if let ppc = tvc.popoverPresentationController {
                    ppc.delegate = self
                    ppc.sourceView = cell
                    ppc.sourceRect = CGRect(x: cell.bounds.minX + cell.bounds.width / 5, y: cell.bounds.minY, width: 50, height: 50 )
                }

            }
        }
    }

screenshot, how it looks like now

关于ios - 如何在 Swift 中为 View 集合中的每个单元格添加弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52905996/

相关文章:

ios - SWRevealViewController 隐藏后视 Controller

ios - UICollectionView 在分页期间不更新单元格

swift - 位置管理器上的计时器

ios - 滚动离开屏幕(即不可见)时如何访问 CollectionView header

python - iOS 6 : libpython2. 7.a初始化导入错误

ios - iPad 1 "400 BadRequest"与 sendAsynchronousRequest,所有工作在 iPad 3

SwiftUI 测试版 7 : Trigger `List` to scroll when item added?

ios - UICollectionView 重新加载数据在 swift 4 中不起作用

ios - 随机 UICollectionView 网格

ios - 如何检测连续相互接触的相同类型节点(SpriteKit)