我有一个 UIScrollView
包括一些组件,其中一个组件是 UITextView
,我想要的是让 UITextView
动态扩展 UIScrollView
,其实我用的是autoLayout
,所以这段代码不起作用:
CGRect frame = self.detailTextView.frame;
frame.size.height = self.detailTextView.contentSize.height;
self.detailTextView.frame = frame;
scroll.contentSize = CGSizeMake(scroll.contentSize.width,
300 + self.detailTextView.frame.size.height);
[self.detailTextView setFrame:frame];
我想要的是用 Storyboard元素帮助我做到这一点。
最佳答案
不要设置框架,而是将 TextView 的高度约束之一拖到 Controller 中以创建 socket 。然后在 viewWillLayoutSubviews 中抓取 contentSize 并设置 TextView 高度约束的常量。这应该完成您在上面的代码中对框架所做的事情。只需确保您的 TextView 从所有边缘到 ScrollView 都有约束,以便 ScrollView 可以正确调整自身大小。
关于ios - UITextView 使用自动布局动态扩展为 ScrollView 内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17040418/