我正在用cs193p快速学习,我遇到了UITextView.sizeThatFits(...)
问题。它应该为popover视图返回一个推荐的大小以显示一个[int]数组作为文本。正如你在Paul Hegarty的例子中看到的(https://youtu.be/gjl2gc70YHM?t=1h43m17s),他得到了完美适合的没有滚动条的弹出式窗口。我使用几乎这是在此讲学相同的代码,而是我这有:为什么sizeThatFits()返回的尺寸太小?
的text
字符串等于[100]
,但sizeThatFits()
方法返回一个尺寸过小即使有足够的可用空间,也可以很好地显示它。 它越来越好一点,我添加了一些文本之后,但仍不够精确,并与滚动条:
这里就是大小被设置代码的一部分:
override var preferredContentSize: CGSize {
get {
if textView != nil && presentingViewController != nil {
// I've added these outputs so I can see the exact numbers to try to understand how this works
print("presentingViewController!.view.bounds.size = \(presentingViewController!.view.bounds.size)")
print("sizeThatFits = \(textView.sizeThatFits(presentingViewController!.view.bounds.size))")
return textView.sizeThatFits(presentingViewController!.view.bounds.size)
} else { return super.preferredContentSize }
}
set { super.preferredContentSize = newValue }
}
我应该怎么做才能像讲座一样工作?
感谢您的咨询。我的'UITextView'已经被限制在页边距: 'Text View.top =顶部布局Guide.bottom; Text View.leading = leadingMargin; Text View.trailing = trailingMargin; Bottom Layout Guide.top = Text View.bottom;' 我已将它更改为 'Text View.leading = leadingMargin - 20; Text View.trailing = trailingMargin + 20;' (顶部和底部保持不变),现在它工作正常。 所以确切的问题是,我认为我应该将我的'UITextView'约束为由Xcode(包含页边距)而不是屏幕边缘所建议的蓝线。 – smocer