如何将 UIImage 正确地锚定到位于自定义单元格中的 UIView 中?
How to anchor a UIImage correctly into UIView located in a Custom Cell?
所以我为 UITableViewController
创建了一个自定义单元格。在这个自定义单元格中,我创建了一个 UIView,我给它一个 UIImageView
和一个 UILabel
。我遇到的问题是图像出现在我的 UIView
后面。 UIImageView
和 UILabelView
的容器都在正确的位置,但是当我将 UIImage
传递到 UIImageView
时,图像出现在后面但没有尽管有锚定,但要把它放在正确的位置。
当前结果:
我为 UIViews, UIImageView
和 UILabel
设置了背景颜色以显示它们的放置位置。附件是它目前的样子的截图。
在屏幕截图中。
粉色方块是UIImageView
。
绿色矩形是 UILabel
。
蓝色矩形是 UIImageView
和 UILabel
也被锚定的 UIView。
图片位于这些视图的后面。
class CustomCell: UITableViewCell{
var cellView: UIView = {
var cellV = UIView()
cellV.backgroundColor = UIColor(red: 0, green: 0, blue: 170, alpha: 0.8)
cellV.addShadow()
return cellV
}()
var mainImage: UIImageView = {
var img = UIImageView()
img.contentMode = .scaleAspectFit
img.backgroundColor = UIColor(red: 110, green: 0, blue: 12, alpha: 0.3)
return img
}()
var imageLabel: UILabel = {
var lbl = UILabel()
lbl.backgroundColor = .green
return lbl
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUp()
}
func setUp(){
self.addSubview(cellView)
cellView.addSubview(mainImage)
cellView.addSubview(imageLabel)
cellView.anchor(top: self.topAnchor,
leading:self.leadingAnchor,
bottom: self.bottomAnchor,
trailing: self.trailingAnchor,
centerXaxis: nil,
centerYaxis: nil,
padding: .init(top: 4, left: 8, bottom: 4, right: 8))
mainImage.anchor(top: nil,
leading: cellView.leadingAnchor,
bottom: nil,
trailing: nil,
centerXaxis: nil,
centerYaxis: cellView.centerYAnchor,
padding: .init(top: 2, left: 2, bottom: 2, right: 0),
size: .init(width: 55, height: 55) )
imageLabel.anchor(top: cellView.topAnchor,
leading: mainImage.trailingAnchor,
bottom: cellView.bottomAnchor,
trailing: trailingAnchor,
centerXaxis: nil,
centerYaxis: nil,
padding: .init(top: 4, left: 8, bottom: 4, right: 8))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
正在为自定义单元格提供图像以进行显示。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: customCellID, for: indexPath) as? CustomCell else {fatalError("Couldn't make it work")}
let profileCard = profile[indexPath.row]
let path = getDocumentsDirectory().appendingPathComponent(profileCard.mainImage)
cell.imageView?.image = UIImage(contentsOfFile: path.path)
cell.imageLabel.text = profileCard.imageLabel
return cell
}
您正在将图像设置到 cell 的 imageView。您应该改为设置 mainImage 的图像:
cell.mainImage.image = UIImage(contentsOfFile: path.path)
所以我为 UITableViewController
创建了一个自定义单元格。在这个自定义单元格中,我创建了一个 UIView,我给它一个 UIImageView
和一个 UILabel
。我遇到的问题是图像出现在我的 UIView
后面。 UIImageView
和 UILabelView
的容器都在正确的位置,但是当我将 UIImage
传递到 UIImageView
时,图像出现在后面但没有尽管有锚定,但要把它放在正确的位置。
当前结果:
我为 UIViews, UIImageView
和 UILabel
设置了背景颜色以显示它们的放置位置。附件是它目前的样子的截图。
在屏幕截图中。
粉色方块是UIImageView
。
绿色矩形是 UILabel
。
蓝色矩形是 UIImageView
和 UILabel
也被锚定的 UIView。
图片位于这些视图的后面。
class CustomCell: UITableViewCell{
var cellView: UIView = {
var cellV = UIView()
cellV.backgroundColor = UIColor(red: 0, green: 0, blue: 170, alpha: 0.8)
cellV.addShadow()
return cellV
}()
var mainImage: UIImageView = {
var img = UIImageView()
img.contentMode = .scaleAspectFit
img.backgroundColor = UIColor(red: 110, green: 0, blue: 12, alpha: 0.3)
return img
}()
var imageLabel: UILabel = {
var lbl = UILabel()
lbl.backgroundColor = .green
return lbl
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setUp()
}
func setUp(){
self.addSubview(cellView)
cellView.addSubview(mainImage)
cellView.addSubview(imageLabel)
cellView.anchor(top: self.topAnchor,
leading:self.leadingAnchor,
bottom: self.bottomAnchor,
trailing: self.trailingAnchor,
centerXaxis: nil,
centerYaxis: nil,
padding: .init(top: 4, left: 8, bottom: 4, right: 8))
mainImage.anchor(top: nil,
leading: cellView.leadingAnchor,
bottom: nil,
trailing: nil,
centerXaxis: nil,
centerYaxis: cellView.centerYAnchor,
padding: .init(top: 2, left: 2, bottom: 2, right: 0),
size: .init(width: 55, height: 55) )
imageLabel.anchor(top: cellView.topAnchor,
leading: mainImage.trailingAnchor,
bottom: cellView.bottomAnchor,
trailing: trailingAnchor,
centerXaxis: nil,
centerYaxis: nil,
padding: .init(top: 4, left: 8, bottom: 4, right: 8))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
正在为自定义单元格提供图像以进行显示。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: customCellID, for: indexPath) as? CustomCell else {fatalError("Couldn't make it work")}
let profileCard = profile[indexPath.row]
let path = getDocumentsDirectory().appendingPathComponent(profileCard.mainImage)
cell.imageView?.image = UIImage(contentsOfFile: path.path)
cell.imageLabel.text = profileCard.imageLabel
return cell
}
您正在将图像设置到 cell 的 imageView。您应该改为设置 mainImage 的图像:
cell.mainImage.image = UIImage(contentsOfFile: path.path)