ios - 如何从 IOS 中的 UICollectionViewCell 初始化弹出 View

标签 ios popover uistoryboardsegue uicollectionviewcell collectionview

我一直在跟踪一个使用 Storyboard的 iPad 应用程序,而且我已经坚持了好几天。如何通过在 Collection View 中选择一个单元格来启动 popover segue?主要问题是克服弹出窗口必须锚定到 View 的错误。

该方法似乎是在 View 中放置一个虚拟的popoverAnchorButton(隐藏,禁用),在 Storyboard中创建一个从它到弹出 View 的转场,将其定位在didSelectItemAtIndexPath,然后是 [self performSegue]。代码如下所示:

- (IBAction)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
    UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
    CGRect anchorRect = CGRectMake(cell.center.x, cell.center.y, 1.0f, 1.0f);
    self.popoverAnchorButton.frame = anchorRect;
    [self performSegueWithIdentifier:@"popoverSegue" sender:self];
}

这适用于应用程序其他地方的 TableView ,因为 Storyboard 让我可以在 View 中放置一个按钮,用作 anchor 。但是 Xcode 不允许我将按钮或任何其他合适的 View 放入 Storyboard 中的 Collection View 中,因此我无法创建转场。以编程方式创建按钮没有帮助,因为我无法从中构建 UIStoryboardSegue,并且来自 Controller 的任何手动 segue 都会给出与缺少 anchor 相同的错误。有什么想法吗?

我认为另一条路径可能是跳过 segues 并以编程方式实例化 popover View Controller ,但这里的障碍是一个错误,因为我创建的 popover View (因为我使用的是 Storyboard)没有 xib。我是否必须为此弹出 View 创建一个单独的 xib 文件?那是唯一的选择吗?

最佳答案

如果您有兴趣获取 Collection View 中当前选定单元格的 CGRect,您可以使用:

CGRect rect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;

之后,您可以使用 UIPopoverController 的 presentPopoverFromRect:inView:permittedArrowDirections:animated: 从该 rect 显示弹出框。

是的,如果 VC 具有关联的 Storyboard标识符,您始终可以从 Storyboard动态加载 VC:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"YourStoryboardName" bundle:nil];
UIViewController* vc = [storyboard instantiateViewControllerWithIdentifier:@"YourIdentifier"];

如果您从 Storyboard本身加载的 VC 调用代码,您可以使用:

UIViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"YourIdentifier"];

关于ios - 如何从 IOS 中的 UICollectionViewCell 初始化弹出 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566387/

相关文章:

javascript - SapUI5 Popover箭头未​​指向设置offsetY后单击的依赖元素

swift - 执行 segue 时的异步检查

ios - Unwind segues 不适用于模态视图 Controller

ios - 检测 Alamofire 中缓存的响应

javascript - 等到弹出窗口被销毁?

ios - 如何将 plist 项输出到数组中

javascript - Bootstrap 弹出窗口,关闭按钮

ios - 我可以将自定义转场用于非视觉目的吗?

ios - NSAttributedString 在本地化时是否仍然保留其属性?

ios - 检测表在 iOS 13 上被取消