![](/img/trans.png)
[英]How can I keep an aspect ratio of UITableViewCell using Auto layout?
[英]Auto layout for UITableViewCell with ratio imageView
我有一个imageView,高度= 0.5 *宽度&将4个边缘固定到cell.contentView。 如何将高度自动调整为imageView高度的单元格。
例如:
iPhone5 : cell size = image size = 320 x 160
iPhone6+: cell size = image size = 414 x 207
您可以在InterfaceBuilder中使用适当的乘数为UIImageView设置宽高比约束。 并使用以下代码计算像元高度:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0; //your estimated height
尝试实施-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
return screenWidth*0.5 ;
}
由于使用的引脚是图像视图布局的约束,因此在运行时更改框架不会更改其框架。 您可以按照以下步骤在运行时更改框架:
UIImageView
的高度和宽度(考虑到iPhone 6+是支持的最高分辨率)。 IBOutlet
。 awakeFromNib
单元格类的awakeFromNib
方法中,可以使用[constraintInstance setConstant:valueForTheConstraint]
设置两个约束的值。 或者,您可以尝试为图像视图的高度和宽度设置纵横比。
试试这个
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UIScreen mainScreen].bounds.size.width/2 ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.