• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

带有自动布局的 UIScrollView 内的 UITextView

用户头像
it1352
帮助1

问题说明

我试图将 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?

正确答案

#1

经过几天的研究和接触 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.

  1. 在 Storyboard 的主视图中添加 UIScrollView
  2. 在 UIScrollView 中添加 UIView
  3. 在 UIView 内添加 UITextView(步骤 2 中添加的视图)
  4. 确保 UITextView 的启用滚动"未选中
  5. 在 UIScrollView 上添加 4 个约束(前导、尾随、顶部、底部)
  6. 在 UIView(步骤 2 中添加的视图)上添加 4 个约束(前导、尾随、顶部、底部)
  7. 在 UIView(第 2 步中添加的视图)和主视图上添加等宽"约束
  8. 在 UITextView 上添加 5 个约束(前导、尾随、顶部、底部、高度).完成此步骤后,您不应收到任何有关约束的错误和警告.
  9. 在 ViewController 上添加 UITextView 高度约束 IBOutlet.@property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; 并在 Storyboard 中连接
  10. 以编程方式更改 UITextView 高度约束.self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, CGFLOAT_MAX)].height;
  1. Add UIScrollView inside the main view in Storyboard
  2. Add UIView inside the UIScrollView
  3. Add UITextView inside the UIView (the view added in step 2)
  4. Make sure "Scrolling Enabled" of UITextView is unchecked
  5. Add 4 constraints (leading, trailing, top, bottom) on UIScrollView
  6. Add 4 constraints (leading, trailing, top, bottom) on UIView (the view added in step 2)
  7. Add "Width Equally" constraint on UIView (the view added in step 2) and the main view
  8. Add 5 constraints (leading, trailing, top, bottom, height) on UITextView. After this step you shouldn't get any errors and warnings on constraints.
  9. Add UITextView height constraint IBOutlet on the ViewController. @property (nonatomic, weak) IBOutlet NSLayoutConstraint *textViewHeightConstraint; and connect it in Storyboard
  10. 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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tangicbba
系列文章
更多 icon
同类精品
更多 icon
继续加载