2011-06-17 63 views
7

我試圖居中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設備處於什麼方位有誰知道正確的方法來做到這一點?

回答

8

添加backcover_imageView到小區的contentView後,用此來居中:

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

我選擇了'backcover_imageview.center = CGPointMake(cell.contentView.bounds.size.width/2,backcover_imageview.center.y);'爲我工作。感謝您的建議! – 2011-06-17 18:13:27

1

你可以嘗試指定單元格的中心屬性爲圖像視圖的屬性嗎?

像這樣的事情

imageView.center = cell.center; 

然後將其添加到單元格。

12

我增加了兩個自動尺寸口罩和使用您的代碼和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); 
+0

這對我來說非常合適。 – newenglander 2013-05-16 14:04:26

+0

你爲我節省了好幾個小時!非常感謝! – Septronic 2015-11-16 09:00:55

相關問題