UIPopoverPresentationController 错位

UIPopoverPresentationController misplaced

我有 table 个项目 (UITableViewController),我在其中使用可自定义的单元格展示项目。在每个单元格的左侧,我有一个缩略图图像,当您单击它时(准确地说是在图像上方的按钮上),会出现一个弹出窗口,应该显示放大的图像。它仅适用于第一个单元格:

单击下面的单元格会显示错误移动的弹出窗口,并且当您从 table 的顶部到底部移动时,错位会增加:

我正在设置 UITableViewController 中每个单元格的块:

 - (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Get a new or recycled cell
BNRItemCell *cell =
    [tableView dequeueReusableCellWithIdentifier:@"BNRItemCell"
                                forIndexPath:indexPath];

// Set the text on the cell with the description of the item
// that is the nth index of items, where n = row this cell
// will appear in on the tableview
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRItem *item = items[indexPath.row];

// Configure the cell with the BNRItem
cell.nameLabel.text = item.itemName;
cell.serialNumberLabel.text = item.serialNumber;
cell.valueLabel.text = [NSString stringWithFormat:@"$%d", item.valueInDollars];

cell.thumbnailView.image = item.thumbnail;

cell.actionBlock = ^{
    NSLog(@"Going to show image for %@", item);

    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
        NSString *itemKey = item.itemKey;

        // if there is no image, we don't need to display anything
        UIImage *img = [[BNRImageStore sharedStore] imageForKey:itemKey];
        if (!img) {
            return;
        }

        BNRImageViewController *ivc = [[BNRImageViewController alloc] init];
        ivc.image = img;

        ivc.modalPresentationStyle = UIModalPresentationPopover;
        ivc.preferredContentSize = CGSizeMake(380, 300);
        CGRect frame = [self.view convertRect:cell.thumbnailView.bounds
                                     fromView:cell.thumbnailView];
        // frame.origin.y -= 150;

        UIPopoverPresentationController *popoverController = ivc.popoverPresentationController;
        popoverController.permittedArrowDirections = UIPopoverArrowDirectionUp;
        popoverController.sourceView = cell.thumbnailView;
        popoverController.sourceRect = frame;

        [self.navigationController presentViewController:ivc animated:YES completion:nil];


    }
};

return cell;

}

点击可自定义视图上的按钮后执行块 UITableViewCell:

@implementation BNRItemCell

- (IBAction)showImage:(id)sender
{
if (self.actionBlock) {
    self.actionBlock();
}
}

@end

actionBlockBNRItemCell

property

如有任何帮助,我们将不胜感激。

试试这样的:

CGRect frame = [cell.view convertRect:cell.thumbnailView.frame toView:self.view];

这里的技巧是boundsframe之间的区别。在按钮的情况下,当您查看其父视图时,它的框架就是它所在的位置(例如 42,42)。但是,边界是按钮相对于自身及其自身坐标 (0,0) 的位置。

不要笑我的画(我不是设计师)但这可能会有所帮助:

你问的是按钮(或者在你的情况下是 UIImage)"where are you relative to the superview"(在这种情况下基本上是整个屏幕)。您使用 convertRect: toView: 执行此操作。您将缩略图(不是其边界)的 frame 转换为其坐标在超级视图上的位置。