【问题标题】:Arrow of UIModalPresentationPopover does not point to UICollectionViewCellUIModalPresentationPopover 的箭头不指向 UICollectionViewCell
【发布时间】:2018-02-01 17:42:16
【问题描述】:

我正在尝试显示指向UICollectionViewCell 的详细视图弹出框。弹出框总是从左上角显示。如何让它将箭头指向选定的单元格?代码如下:

-(void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JCCollectionData *favorite  = [self.dataSource objectAtIndex:indexPath.row];

    JCCollectionCellView *cell      = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];

    JCContactDetailViewController *contactDetail = [[JCContactDetailViewController alloc] init];
    contactDetail.Contact = favorite;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:contactDetail];

    nav.modalPresentationStyle = UIModalPresentationPopover;
    nav.popoverPresentationController.delegate = self;
    nav.popoverPresentationController.sourceView = cell;
    nav.popoverPresentationController.sourceRect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
    nav.preferredContentSize = CGSizeMake(500, 400);
    nav.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionUp;
    [self presentViewController:nav animated:YES completion:nil];
}

【问题讨论】:

    标签: ios objective-c uicollectionview


    【解决方案1】:

    这是因为在这两行中:

    nav.popoverPresentationController.sourceView = cell;
    nav.popoverPresentationController.sourceRect = [collectionView layoutAttributesForItemAtIndexPath:indexPath].frame;
    

    您是说源视图是单元格,然后将单元格的框架作为源矩形,这将与它的超级视图而不是自身相关。

    相反,您可以cell.bounds 或只是构建一个 CGRect,它的原点位于 0,0 并且单元格框架的大小。

    编辑

    我刚刚注意到别的东西。你有这行:

    JCCollectionCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellID forIndexPath:indexPath];
    

    这是使单元格的一个新实例出列,而没有得到作为选择源的单元格。

    相反,您应该这样做:

    JCCollectionCellView *cell = (JCCollectionCellView*)[collectionView cellForItemAtIndexPath:indexPath];
    

    这应该会让你得到生成选择的实际单元格。

    (我认为语法是正确的,但我的 Objective C 变得有点生疏了,因为我全职使用 Swift。不过应该足以让你指出正确的方向。)

    【讨论】:

    • 我刚刚重新尝试了 cell.bounds 并尝试了 CGRectMake(0, 0, 40, 40)。在这两种情况下,从左上角显示的结果仍然相同。 ist 是否与复杂的视图层次结构有关?
    • 抱歉只看到评论。我刚刚注意到别的东西,所以我会更新答案。
    • 哇!我自己都瞎了眼。非常感谢!
    猜你喜欢
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2020-04-14
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多