我有一个具有以下代码的UITextView子类:
@IBDesignable class UITextViewFixed: UITextView {
override func layoutSubviews() {
super.layoutSubviews()
setup()
}
func setup() {
textContainerInset = .zero
textContainer.lineFragmentPadding = 0
var b = bounds
let h = sizeThatFits(CGSize(
width: bounds.size.width,
height: CGFloat.greatestFiniteMagnitude)
).height
b.size.height = h
bounds = b
contentInset = .zero
}
}
它取自this堆栈溢出答案,用于生成没有任何文本填充的UITextViews。不幸的是,当我调用self.view.layoutIfNeeded()
来调整保存我的文本视图的容器视图时,会发生递归循环。它先调用layoutSubviews()
,然后再调用setup()
,然后再调用layoutSubviews()
,依此类推。这种情况一直持续到导致堆栈溢出为止。我该如何阻止呢?
答案 0 :(得分:1)
我通过删除此位对其进行了修复:
var b = bounds
let h = sizeThatFits(CGSize(
width: bounds.size.width,
height: CGFloat.greatestFiniteMagnitude)
).height
b.size.height = h
bounds = b
contentInset = .zero