ios - 如何调整 UIView 的大小以适应其内容?
问题描述
我有一个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
}
解决方案
你需要这些约束
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()
推荐阅读
- php - Laravel 使用端口(Nginx)登录到 url 后重定向
- typescript - TypeScript:检查对象中的键/在运行时缩小
- angular - 如何在 Angular 中将 [(ngModel)] 与 FormGroup 一起使用
- java - 路径变量旁边的Spring Boot精确逗号
- c# - ASP.Net Core Blazor:如何根据标头值加载不同的 _Host.cshtml 文件
- r - 在“素食主义者”中排列网络(作为邻接矩阵) - 我如何获取网络统计信息?
- oracle - Flyway 是否支持 kerberos 身份验证?
- powershell - 从对象字符串中获取值以存储在属性内部
- python - 如何避免 conda activate base 在我的 VS Code 编辑器中自动执行?
- python - Python从url解析JSON不完整