我有一个简单的自定义UITableViewCell,它左侧有个人资料图片,右边有标题和detailsLabel。我使用了Auto Layout约束来设置屏幕上的所有视图。但是细节可用的文字很短,而且个人资料图像被切断了。
让我知道如何解决它。我可以将图像缩小,比标题和标签的高度短,但是我想要大图像。
// adding subviews
contentView.addSubview(profileImageView)
contentView.addSubview(nameLabel)
contentView.addSubview(jobTitleDetailedLabel)
// constraint for the views
profileImageView.topAnchor.constraint(equalTo:self.contentView.topAnchor, constant:10).isActive = true
profileImageView.leadingAnchor.constraint(equalTo:self.contentView.leadin gAnchor, constant:10).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant:50).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant:50).isActive = true
nameLabel.topAnchor.constraint(equalTo:self.contentView.topAnchor, constant:10).isActive = true
nameLabel.leadingAnchor.constraint(equalTo:self.profileImageView.trailingAnchor, constant:10).isActive = true
nameLabel.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor).isActive = true
jobTitleDetailedLabel.topAnchor.constraint(equalTo:self.nameLabel.bottomAnchor).isActive = true
jobTitleDetailedLabel.leadingAnchor.constraint(equalTo:self.profileImageView.trailingAnchor, constant:10).isActive = true
jobTitleDetailedLabel.trailingAnchor.constraint(equalTo:self.contentView.trailingAnchor).isActive = true
jobTitleDetailedLabel.bottomAnchor.constraint(equalTo:self.contentView.bottomAnchor, constant:-10).isActive = true
答案 0 :(得分:0)
只需将jobTitleDetailedLabel高度约束添加到greaterThanOrEqualToConstant
profileImageView高度+ 10作为保证金
因为如果jobTitleDetailedLabel
hight小于Image hight
,则会使其成为小单元格行
self.jobTitleDetailedLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: 60).isActive = true
答案 1 :(得分:0)
确保将单元格的contentView的底部锚点限制为更大,然后是profileImageView的底部锚点和jobTitleDetailedLabel的底部锚点。细胞将根据较大的细胞扩大。
所以你需要删除jobTitleDetailedLabel的底部锚点约束并添加:
contentView.bottomAnchor.constraint(greaterThanOrEqualTo: profileImageView.bottomAnchor, constant: 10).isActive = true
contentView.bottomAnchor.constraint(greaterThanOrEqualTo: jobTitleDetailedLabel.bottomAnchor, constant: 10).isActive = true
还要将nameLabel的contentHuggingPriority设置为高,以确保nameLabel保持其固有高度(文本占用的高度)。
nameLabel.setContentHuggingPriority(UILayoutPriority.defaultHigh, for: .vertical)
最后在故事板中或通过代码 - tableView.rowHeight = UITableViewAutomaticDimension