如何修复单元格的textlabel在文本和图像之间具有不同的间距如何解决此表通过编码创建的问题
class NewMessageCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
}
override func layoutSubviews() {
super.layoutSubviews()
self.imageView?.frame = CGRect(x: 5, y: 10, width: 70, height: 70)
self.imageView?.clipsToBounds = true
self.textLabel?.highlightedTextColor = UIColor.blue
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
答案 0 :(得分:0)
self.imageView?.frame = CGRect(x: 5, y: 10, width: 70, height: 70)
如果您设置UIImageView的边界,那么它将变得一团糟。 UIImageView将自动填充单元格的大小,因此,如果您以编程方式创建视图,则可以控制整个视图的大小。
如果您需要将所有图像设置为新的大小以使其起作用,请使用此功能(我从this question为iOS 10更新了图像:
func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage {
let size = image.size
let widthRatio = targetSize.width / size.width
let heightRatio = targetSize.height / size.height
// Figure out what our orientation is, and use that to form the rectangle
var newSize: CGSize
if(widthRatio > heightRatio) {
newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
} else {
newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio)
}
// This is the rect that we've calculated out and this is what is actually used below
let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
// Actually do the resizing to the rect using the ImageContext stuff
return UIGraphicsImageRenderer(size: newSize).image { ctx in
image.draw(in: rect)
}
}