問(wèn)題描述
我試圖將 UITextView 放置在帶有 AutoLayout 的 UIScrollView 中,但沒(méi)有成功.我試過(guò)的是,
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 上設(shè)置了約束(前導(dǎo)、尾隨、頂部、底部)
- 我在 UITextView 上設(shè)置了約束(頂部、前導(dǎo)、尾部、高度)
- 我創(chuàng)建了 UITextView 高度約束的 IBOutlet
- 我在 viewDidLoad() 中的 UITextView 上設(shè)置了一個(gè)文本(很多會(huì)導(dǎo)致滾動(dòng)的文本)
- 我用下面的代碼設(shè)置了 UITextView 的高度約束.在 viewDidLoad() 和 viewDidLayoutSubviews() 中設(shè)置文本后,我已經(jīng)嘗試過(guò)了,但沒(méi)有成功
self.textViewHeightConstraint.constant = [self.textView sizeThatFits:CGSizeMake(self.textView.frame.size.width, FLT_MAX)].height;
UITextView 正在達(dá)到它的高度,但 UIScrollView 沒(méi)有.有什么我錯(cuò)過(guò)的嗎?
UITextView is getting its height, but UIScrollView isn't. Is there anything I've missed?
推薦答案
經(jīng)過(guò)幾天的研究和接觸 UIScrollView + UITextView + Auto Layout,我成功獲得了一個(gè)完整的 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 內(nèi)添加 UITextView(步驟 2 中添加的視圖)
- 確保 UITextView 的啟用滾動(dòng)"未選中
- 在 UIScrollView 上添加 4 個(gè)約束(前導(dǎo)、尾隨、頂部、底部)
- 在 UIView(步驟 2 中添加的視圖)上添加 4 個(gè)約束(前導(dǎo)、尾隨、頂部、底部)
- 在 UIView(第 2 步中添加的視圖)和主視圖上添加等寬"約束
- 在 UITextView 上添加 5 個(gè)約束(前導(dǎo)、尾隨、頂部、底部、高度).完成此步驟后,您不應(yīng)收到任何有關(guān)約束的錯(cuò)誤和警告.
- 在 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 個(gè)步驟后,您將獲得完整的 UIScrollView 和內(nèi)部的 UITextView,并且會(huì)很開(kāi)心.
After all of these 10 steps, you'll get fully working UIScrollView with UITextView inside and be happy.
這篇關(guān)于帶有自動(dòng)布局的 UIScrollView 內(nèi)的 UITextView的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!