gpt4 book ai didi

ios - 在选定的 UICollectionViewCell 下方显示弹出窗口

转载 作者:搜寻专家 更新时间:2023-11-01 06:25:37 25 4
gpt4 key购买 nike

我正在努力显示应该显示在所选单元格下方的弹出窗口。

我有两个 View Controller ,都包含 UICollectionView。因此,当我从第一个 View Controller 中选择单元格时,我想在所选单元格正下方显示带有动画的弹出窗口。

我已尝试使用以下代码来显示它。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

if self.signupFirstStepVC != nil {
self.signupFirstStepVC?.view.removeFromSuperview()
self.signupFirstStepVC = nil
}

let cell = collectionView.cellForItem(at: indexPath) as! MainCategoryCVC
collectionView.scrollToItem(at: indexPath, at: .top, animated: true)
self.signupFirstStepVC = SignupFirstStepVC.viewController()

let viewFrame = self.signupFirstStepVC?.view.frame

self.signupFirstStepVC?.view.frame = CGRect(x: viewFrame!.origin.x, y: cell.lblCategoryName.frame.maxY, width: collectionView.frame.size.width, height: collectionView.frame.size.height - 100)

UIView.transition(from: cell, to: self.signupFirstStepVC!.view, duration: 0.5, options: .transitionCrossDissolve) { (success) in
self.view.addSubview(self.signupFirstStepVC!.view)
}
}

这段代码有效,但没有像我预期的那样显示正确的 View 。

输出:

Add Hobbies

您可以在上图中看到,我的弹出窗口已添加,但起点从 UICollectionView 的起点开始,而不是 celllblCategoryNamemaxY

预期:

Expected Add Hobbies

最佳答案

需要根据superview转换rect。

let theAttributes = collectionView.layoutAttributesForItem(at: indexPath)
let cellFrameInSuperview = collectionView.convert(theAttributes.frame, to: collectionView.superview)

现在你有了根据它的父 View 的框架。

使用cellFrameInSuperview 属性设置 View Controller 的框架

提示:您应该将 View Controller 添加为 subview Controller ,然后添加 subview 以正确管理 View 生命周期。

编辑

请忽略错别字

self.signupFirstStepVC?.view.frame = CGRect(x: cellFrameInSuperview!.origin.x, y: cellFrameInSuperview.maxY, width: collectionView.frame.size.width, height: collectionView.frame.size.height - 100)    

关于ios - 在选定的 UICollectionViewCell 下方显示弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55721021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com