ios - UIPopoverPresentationController 错位

标签 ios objective-c ipad

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

Image popover looks good for the first row

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

Image popover looks misplaced for other rows

我正在设置 UITableViewController 内的每个单元格的 block :

 - (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;

}

该 block 在单击自定义 View 上的按钮时执行 UITableViewCell :
@implementation BNRItemCell

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

@end
actionBlockpropertyBNRItemCell
任何帮助将不胜感激。

最佳答案

尝试类似:
CGRect frame = [cell.view convertRect:cell.thumbnailView.frame toView:self.view];
这里的诀窍是 bounds 之间的区别和 frame .在按钮的情况下,当您查看它的 super View 时,它的框架就是它所在的位置(例如 42,42)。但是,边界是按钮相对于自身及其坐标 (0,0) 的位置。

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

enter image description here

您正在询问按钮(或在您的情况下为 UIImage)“您相对于 super View 在哪里”(在这种情况下基本上是整个屏幕)。您可以使用 convertRect: toView: .您将缩略图的框架(而不是其边界)转换为其坐标在 super View 上的位置。

关于ios - UIPopoverPresentationController 错位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33789841/

相关文章:

ios - dyld : Library not loaded Reason: no suitable image found. 是否找到:/private/var/mobile/Containers/Bundle/Application…

ios - UIKeyboardTypeDecimalPad 十进制按钮以半宽为上限

objective-c - UIView 在横向时具有纵向尺寸?

javascript - 最佳 Javascript 地理图表/ map 库

ios - 数组中的图像未显示在 UICollectionview 中

ios - 呈现小型 Modal VIewController Objective C

ios - “调整”我当前的 UISwipeGestureRecognizer 代码,使用 UIPanGestureRecognizer 将图像轻弹到 iPhoto 样式

ios - 将 UIWebView 转换为具有动态大小的 UITableViewCell

iphone - 我的应用程序在播放声音时崩溃

ipad - 如何知道 iPad 上的 Youtube 播放器/插件何时全屏显示?