我在我的 tableView header 中添加了一个 View ,其中包含一个 imageView 和一个 textView。 ImageView 在顶角左对齐, TextView 在 ImageView 上延伸到屏幕右侧,如下所示。
textView 可以有动态内容并且有一个排除路径设置如下:
let imagePath = UIBezierPath(rect: imageView.frame)
self.textView.textContainer.exclusionPaths = [imagePath]
我已禁用 TextView 的滚动并在标题 View 中设置了以下约束:
TextView:左 - 8px,右 - 8px,上 - 0px,下 - 8px
ImageView: left - 8px, width - 100px, height 100px, top - 8px, bottom - 大于等于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 的文本少于图像的高度时, View 的大小会增加。
有人知道为什么会这样吗?
最佳答案
我有一个解决办法,因为我自己也遇到过这个问题:)
你看,我正在尝试做一些非常相似的事情,其中 UITextView
的文本应避免 UIImageView
在它的左边。我的代码如下:
let ticketProfileExclusionPath = UIBezierPath(roundedRect: ticketProfilePicture.frame, cornerRadius: Constants.ProfilePictureExclusionRadius)
ticketContent.textContainer.exclusionPaths.append(ticketProfileExclusionPath)
我的结果不是很好:
如您所见,问题依赖于 CGRect
给了ticketContent
UITextView
作为它的排除路径,因为 后者假设给定的 CGRect
被更正为它自己的框架,而不是它的 super View 的。
修复非常简单,需要使用从时间开始就存在的 API (iOS 2.0):
let exclusionPathFrame = convert(ticketProfilePicture.frame, to: ticketContent).offsetBy(dx: Constants.SystemMargin, dy: Constants.Zero)
我们在这里所做的是转换 UIImageView
的框架到 UITextView
的坐标系,因此从 UITextView
的角度提供正确的排除路径.添加的偏移量只是为了对齐我的所有三个 UITableViewCell
的正文 UIView
秒。
convert()
是 UIView
方法。
如您所见,文本现在环绕在 ticketProfilePicture
周围UIImageView
非常好。
希望这对您有所帮助。
关于ios - 如何使用表头 View 中的 exclusionPaths 正确调整 textview 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41845443/