我在表格 View 单元格中附加了一个 UItextView。现在我想像聊天气泡一样在它后面插入一个 UIimage。但是每次滚动 tableview 时,Uitextview 的宽度都会变小,高度会变长。文本的大小也从单行调整为多行。此外,当我滚动回到较早的单元格(例如第一行)时,也会发生同样的事情。 我不明白为什么会这样。
我的代码是: (SampleTableViewCell 是我的自定义 UItableViewCell 类,lblTitle 是我的 UitextView socket 。)
func tableView(tableView: UITableView, willDisplayCell cell: SampleTableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
cell.lblTitle.removeConstraints(cell.lblTitle.constraints)
let stringTitle = carName[indexPath.row] as String //carName is an array
cell.lblTitle.text=stringTitle
cell.lblTitle.sizeToFit()
let h = cell.lblTitle.frame.size.height
let w = cell.lblTitle.frame.size.width
let constraints = NSLayoutConstraint(item: cell.lblTitle, attribute: .Width, relatedBy: .Equal, toItem: cell.lblTitle, attribute: .Height, multiplier: 0, constant: w)
cell.lblTitle.addConstraint(constraints)
let kl = UIImage(imageLiteral: "bubble")
let imageView = UIImageView(frame:CGRectMake(0, 0, w, h))
imageView.image = kl
imageView.alpha = 0.4
cell.lblTitle.addSubview(imageView)
cell.lblTitle.sendSubviewToBack(imageView)
}
最佳答案
是autolayout的问题。
您只是对 textView 进行了高度和宽度限制。 x和y位置呢?
而且你还没有为你的 imageview 设置约束!!
如果您要为一个对象提供约束,那么您需要为与其相关的所有对象提供约束。
所以设置适当的约束,你的问题就会得到解决。
关于ios - 在 tableview 的 UItexView 后面附加一个 UIimage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37177654/