【问题标题】:Attaching an UIimage behind a UItexView in tableview在 tableview 中的 UItexView 后面附加 UIimage
【发布时间】:2016-09-07 17:47:23
【问题描述】:

我在 tableview 单元格中附加了一个 UItextView。现在我想像聊天气泡一样在它后面插入一个 UIimage。但是每次滚动 tableview 时,Uitextview 的宽度都会变小,高度会变长。文本也从单行调整为多行。此外,当我滚动回较早的单元格(例如第一行)时,也会发生同样的事情。 我无法弄清楚为什么会这样。

我的代码是: (SampleTableViewCell 是我的自定义 UItableViewCell 类,lblTitle 是我的 UitextView 出口。)

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)
  }  

【问题讨论】:

    标签: ios swift uitableview uiimageview autolayout


    【解决方案1】:

    是自动布局的问题。

    您只是给 textView 提供了高度和宽度限制。 x 和 y 位置呢?

    而且你还没有为你的 imageview 设置约束!!

    如果你给一个对象施加约束,那么你需要给每个与之相关的对象。

    所以设置适当的约束,你的问题就会得到解决。

    【讨论】:

    • 我已经在创建uiImageView的时候定义了它的大小。问题是每次我滚动 w 和 h 时都会发生变化。如果我在 UItextview 中使用超级视图设置尾随约束,它会解决问题吗?
    【解决方案2】:

    不是以编程方式添加气泡图像视图和约束,而是使用情节提要,您可以先在表格视图单元格中添加图像视图,然后再添加与图像视图相同大小的 UITextView 并添加约束。然后您可以在 cellForRowAtIndexPath 委托中为 lblTitle 的文本和气泡图像添加逻辑.确保将 lblTitle 的背景设置为 ClearColor。

    【讨论】:

    • 但是我的 uitextview 大小会根据它的内容而改变。我将如何相应地更改我的图像视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多