无论方向如何,如何使 UIImageView 在 UITableViewCell 中居中?
我正在尝试使用以下代码将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我添加了两个自动调整大小蒙版,并使用您的代码和 PengOne 的答案得到了很好的结果:
I added two more autoresizing masks and got a good result using your code and PengOne's answer:
将
backcover_imageView
添加到单元格的contentView
后,使用它使其居中:After you add
backcover_imageView
to thecontentView
of the cell, use this to center it:您可以尝试将单元格的中心属性分配给图像视图的中心属性吗?
像这样的东西
然后将其添加到单元格中。
Can you try assigning the center property of cell to that of image view?
Something like this
And then add it to the cell.
您可以使用单元格的中心锚点将图像保持在中心:
You can use center anchors of the cell to keep the image in the center: