【发布时间】:2018-11-02 16:56:50
【问题描述】:
我正在尝试制造泡沫UITableviewCell。单元格有一个UILabel,所有edges 引脚都连接到单元格ContentView 的边缘,UITableView 正在使用AutomaticHeight(基于Label.text)我已将CellBackgroundView 图像设置为下面的函数
func changeImage(_ name: String) -> UIImage? {
guard let image = UIImage(named: name) else { return nil }
return image.resizableImage(withCapInsets: UIEdgeInsets(top: 17, left: 30, bottom: 17, right: 30), resizingMode: .stretch).withRenderingMode(.alwaysTemplate)
}
以上函数在cellforRowAtIndexPath中调用:
cell.backgroundView = UIImageView(image: changeImage("right_bubble"))
我有一个看起来像气泡的背景图片:
问题是我无法更改Cell 的左侧或右侧padding。有没有办法改变填充(可能是左或右)(以区分发送者和接收者)或需要从UITableView更改为UICollectionView?
编辑
我已经提出了一种在自定义单元子类中动态更改约束的解决方案。下面是子类的实现
import UIKit
enum Direction {
case left
case right
}
class MessageCell: UITableViewCell {
@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var messageText: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func prepareForReuse() {
self.layoutIfNeeded()
}
func configureCell(with message: Message, direction: Direction ) {
self.messageText?.text = message.text
switch direction {
case .left:
backgroundImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10).isActive = true
contentView.trailingAnchor.constraint(greaterThanOrEqualTo: backgroundImageView.trailingAnchor, constant: 50).isActive = true
self.backgroundImageView.image = UIImage.changeImage("left_bubble")
self.backgroundImageView.tintColor = .lightGray
case .right:
backgroundImageView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -10).isActive = true
backgroundImageView.leadingAnchor.constraint(greaterThanOrEqualTo: contentView.leadingAnchor, constant: 50).isActive = true
self.backgroundImageView.image = UIImage.changeImage("right_bubble")
self.backgroundImageView.tintColor = .blue
}
self.layoutIfNeeded()
}
}
但当行数超过屏幕高度并需要滚动时,Autolayout 会出现问题。以下是错误:
2018-11-03 19:53:36.478275+0800 VicGithubDM[2352:34611] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x6000006e9090 V:|-(10)-[UILabel:0x7fe37dd3bb70'still have some bugs stil...'] (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>",
"<NSLayoutConstraint:0x6000006ea620 V:[UILabel:0x7fe37dd3bb70'still have some bugs stil...']-(10)-| (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>",
"<NSLayoutConstraint:0x6000006e1d10 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7fe37dd3b750.height == 1.19209e-07 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000006ea620 V:[UILabel:0x7fe37dd3bb70'still have some bugs stil...']-(10)-| (active, names: '|':UITableViewCellContentView:0x7fe37dd3b750 )>
【问题讨论】:
标签: swift uitableview padding collectionview