带有 AutoLayout 的 UIScrollView 内的 UITextView [英] UITextView inside UIScrollView with AutoLayout
问题描述
我试图将 UITextView 放置在带有 AutoLayout 的 UIScrollView 中,但没有运气.我试过的是,
I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,
- 我将 UIScrollView 放在 Storyboard 的主视图中
- 我将 UITextView 放在 Storyboard 的 UIScrollView 中并禁用了滚动启用
- 我在 UIScrollView 上设置了约束(前导、尾随、顶部、底部)
- 我在 UITextView 上设置了约束(顶部、前导、尾随、高度)
- 我创建了 UITextView 高度约束的 IBOutlet
- 我在 viewDidLoad() 中的 UITextView 上设置了一个文本(很多会导致滚动的文本)
- 我使用下面的代码设置了 UITextView 的高度约束.我在 viewDidLoad() 和 viewDidLayoutSubviews() 中设置文本后立即尝试了它,但没有运气
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height;
UITextView 获得了它的高度,但 UIScrollView 没有.有什么我遗漏的吗?
UITextView is getting its height, but UIScrollView isn't. Is there anything I've missed?
推荐答案
经过几天的研究并开始接触 UIScrollView + UITextView + Auto Layout,我成功地获得了一个完全可用的 UIScrollView.我想分享我的解决方案,以防有人遇到同样的情况.
After a few days of research and getting my hands dirty with UIScrollView + UITextView + Auto Layout, I successfully got a fully working UIScrollView. I want to share my solution just in case someone might stuck on the same situation.
- 在 Storyboard 的主视图中添加 UIScrollView
- 在 UIScrollView 中添加 UIView
- 在 UIView 中添加 UITextView(步骤 2 中添加的视图)
- 确保未选中 UITextView 的滚动启用"
- 在 UIScrollView 上添加 4 个约束(前导、尾随、顶部、底部)
- 在 UIView(第 2 步中添加的视图)上添加 4 个约束(前导、尾随、顶部、底部)
- 在 UIView(步骤 2 中添加的视图)和主视图上添加宽度相等"约束
- 在 UITextView 上添加 5 个约束(前导、尾随、顶部、底部、高度).在此步骤之后,您不应收到任何有关约束的错误和警告.
- 在 ViewController 上添加 UITextView 高度约束 IBOutlet.
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint;
并在 Storyboard 中连接 - 以编程方式更改 UITextView 高度约束.
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
- Add UIScrollView inside the main view in Storyboard
- Add UIView inside the UIScrollView
- Add UITextView inside the UIView (the view added in step 2)
- Make sure "Scrolling Enabled" of UITextView is unchecked
- Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
- Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
- Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
- Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
- Add UITextView height constraint IBOutlet on the ViewController.
@property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint;
and connect it in Storyboard - Change the UITextView height constraint programmatically.
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
在完成所有这 10 个步骤之后,您将完全使用 UIScrollView 并在其中使用 UITextView 并感到高兴.
After all of these 10 steps, you'll get fully working UIScrollView with UITextView inside and be happy.
这篇关于带有 AutoLayout 的 UIScrollView 内的 UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!