无论方向如何,如何使 UIImageView 在 UITableViewCell 中居中?

发布于 2024-11-16 08:59:47 字数 1028 浏览 3 评论 0原文

我正在尝试使用以下代码将 UIImageView 在 UITableViewCell 中横向居中:

        // self.cover_image is a UIImage variable
        // cell is a UITableCellView

        UIImageView* backcover_imageview = [[[UIImageView alloc] initWithImage:self.cover_image] autorelease];
        [backcover_imageview sizeToFit];

        //center image
        //float xPos = cell.contentView.frame.size.width - (self.cover_image.size.width/2);

        //TODO: need better solution
        //Eyeballing attempt:
        float xPos = self.cover_image.size.width - 25;
        CGRect bookcover_frame = backcover_imageview.frame;
        bookcover_frame.origin.x = xPos;
        backcover_imageview.frame = bookcover_frame;
        backcover_imageview.tag = 9000;
        backcover_imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

        [cell.contentView addSubview:backcover_imageview];

我正在尝试将 UIImageView 在 UITableCellView 中居中,无论 iPad 或 iPhone 设备处于什么方向。有谁知道正确的方法吗这?

I'm trying to center a UIImageView width-wise in a UITableViewCell with the following code:

        // self.cover_image is a UIImage variable
        // cell is a UITableCellView

        UIImageView* backcover_imageview = [[[UIImageView alloc] initWithImage:self.cover_image] autorelease];
        [backcover_imageview sizeToFit];

        //center image
        //float xPos = cell.contentView.frame.size.width - (self.cover_image.size.width/2);

        //TODO: need better solution
        //Eyeballing attempt:
        float xPos = self.cover_image.size.width - 25;
        CGRect bookcover_frame = backcover_imageview.frame;
        bookcover_frame.origin.x = xPos;
        backcover_imageview.frame = bookcover_frame;
        backcover_imageview.tag = 9000;
        backcover_imageview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;

        [cell.contentView addSubview:backcover_imageview];

I'm trying to center the UIImageView in a UITableCellView regardless of what orientation the iPad or iPhone device is in. Does anyone know the right way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何处潇湘 2024-11-23 08:59:47

我添加了两个自动调整大小蒙版,并使用您的代码和 PengOne 的答案得到了很好的结果:

playImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[cell.contentView addSubview:playImage];
playImage.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

I added two more autoresizing masks and got a good result using your code and PengOne's answer:

playImage.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[cell.contentView addSubview:playImage];
playImage.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
无戏配角 2024-11-23 08:59:47

backcover_imageView 添加到单元格的 contentView 后,使用它使其居中:

imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);

After you add backcover_imageView to the contentView of the cell, use this to center it:

imageView.center = CGPointMake(cell.contentView.bounds.size.width/2,cell.contentView.bounds.size.height/2);
半衾梦 2024-11-23 08:59:47

您可以尝试将单元格的中心属性分配给图像视图的中心属性吗?

像这样的东西

imageView.center = cell.center;

然后将其添加到单元格中。

Can you try assigning the center property of cell to that of image view?

Something like this

imageView.center = cell.center;

And then add it to the cell.

秋风の叶未落 2024-11-23 08:59:47

您可以使用单元格的中心锚点将图像保持在中心:

image.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
image.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true

You can use center anchors of the cell to keep the image in the center:

image.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
image.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文