如何根据UIImageView高度设置动态UIImageView高度以及UITableViewCell高度?

时间:2019-03-28 11:10:04

标签: ios objective-c uitableview uiimageview ios-autolayout

想要根据图像大小动态设置UIImageView大小,并且还想根据上述UIImageView高度设置UITableViewCell高度,该高度在UITableViewCell中使用Objective c中的AutoLayout。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return UITableViewAutomaticDimension;
}

下面是我在cellForRowAtIndexPath方法下的代码:

[cellRecImg.imgMessage sd_setImageWithURL:[NSURL URLWithString:[dictMessage objectForKey:@"msg_attachment"]] placeholderImage:[UIImage imageNamed:@"Image-33"]];
CGFloat imageHeight = cellRecImg.imgMessage.image.size.height;
CGFloat imageWidth = cellRecImg.imgMessage.image.size.width;
float ratio = cellRecImg.imgMessage.frame.size.width / cellRecImg.imgMessage.frame.size.height;
float newHeight = imageHeight / ratio;
float newWidth = imageWidth / ratio;
cellRecImg.constImageHeight.constant = newHeight;
cellRecImg.constImageWidth.constant = newWidth;
[self.view layoutIfNeeded];

This is my UITableViewCell's xib design where can you see the view' constant. This is my UIImageView, here you can see the constants of UIImageView's. This the result I got from simulator.

在此结果图像中,您可以看到UIImageView从UIImage到UIImageView边缘创建了许多额外的间隙。其实我不希望有这个差距,也想根据UIImageView的高度动态设置UITableViewRow的高度。

请使用目标c语言进行回答

0 个答案:

没有答案
相关问题