我在 tableView 标题中添加了一个视图,其中包含 imageView 和 textView。图像视图在顶角左对齐,文本视图在图像视图上延伸到屏幕的右侧,如下所示。
textView 可以有动态内容,并具有如下设置的排除路径:
let imagePath = UIBezierPath(rect: imageView.frame)
self.textView.textContainer.exclusionPaths = [imagePath]
我已禁用文本视图的滚动,并在标题视图内设置了以下约束:
TextView:左 - 8px,右 - 8px,顶部 - 0px,底部 - 8px
ImageView:左 - 8px,宽 - 100px,高 100px,顶部 - 8px,底部 - 大于或等于 8px
在我的 textView 填充动态文本后,我添加了以下代码:
if let headerView = self.tableView.tableHeaderView {
let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var headerFrame = headerView.frame
if height != headerFrame.size.height {
headerFrame.size.height = height
headerView.frame = headerFrame
self.tableView.tableHeaderView = headerView
}
}
它调整标题的大小。但是,当 textView 的文本少于图像的高度时,视图的大小会增大。
Example of three lines of text:
Example of six lines of text:
Example of enough text to pass imageview:
有谁知道为什么会发生这种情况?