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?
backcover_imageview.center = CGPointMake(cell.contentView.bounds.size.width/2,backcover_imageview.center.y);
which worked for me. Thanks for your suggestion! – Manchukuo