【问题标题】:How do you resize a UIView to fit its contents?如何调整 UIView 的大小以适应其内容?
【发布时间】:2021-05-21 19:59:08
【问题描述】:

我有一个UIView,其中包含两个标签。我希望视图调整大小,使其包含每个标签中的所有文本,但文本溢出视图。这是我的代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: K.reuseIdentifier, for: indexPath) as! NoteTableViewCell
       
    cell.titleLabel?.text = titleArray[indexPath.row]
    cell.mainTextLabel?.text = noteArray[indexPath.row]
    cell.titleLabel?.font = UIFont(name:"HelveticaNeue-Bold", size: 18.0)
    cell.cardView?.sizeToFit()
    cell.cardView?.layer.shadowOpacity = 1
    cell.cardView?.layer.shadowOffset = .zero
    cell.cardView?.layer.shadowRadius = 1
    cell.cardView?.layer.cornerRadius = 8
    return cell
}

结果如下:

【问题讨论】:

    标签: ios swift uiview constraints


    【解决方案1】:

    你需要这些约束

    CardView // hook it's top,bottom,leading and trailing to parent View
    
            |
      - titleLabel -  // hook it's top , leading and trailing to Card View  
            | 
      - mainTextLabel - // hook it's top to titleLabel , leading and trailing and bottom to Card View  
            |
    

    然后删除

    cell.cardView?.sizeToFit()
    

    【讨论】:

      猜你喜欢
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多