问题描述
我试图将 UITextView 放置在带有 AutoLayout 的 UIScrollView 中,但没有成功.我试过的是,
I am trying to place UITextView inside UIScrollView with AutoLayout with no luck. What I have tried is,
- 我将 UIScrollView 放在 Storyboard 的主视图中
- 我在 Storyboard 中的 UIScrollView 中放置了 UITextView 并禁用了 Scrolling Enabled
- 我在 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.
这篇关于带有自动布局的 UIScrollView 内的 UITextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)