2014-03-14 52 views
4

我正在做一個測試項目,並且遇到了UITextView的問題。iOS7 UITextView scrollEnabled = YES高度

我在文本視圖中動態獲取文本的內容大小,然後在需要時增加其高度。當高度達到我設定的閾值時,我將設置scrollEnabled = YES來啓用滾動。奇怪的事情似乎在下面的屏幕截圖發生,因爲所示:

臨睡前新的生產線,並實現滾動:

enter image description here

進入下一個字符,這將使滾動後

enter image description here

之後,再次輸入另一個字符後,文本視圖將在滾動啓用後變爲正常(實際上高度保持與之前的s一樣creen出手,我根據內容的大小改變高度,使之成爲同一高度之前啓用滾動):

enter image description here

任何人碰到這個問題,能夠解決呢?如果這是一個iOS7的bug,還有什麼建議可以創建一個消息輸入文本框?我想知道以前的iOS版本是否有這個問題。

編輯:

好像當TextView中的scrollEnabled是YES,改變textview.frame.size.height發生此問題,那麼高度將重置爲初始高度(如高度在Interface Builder中設置)。不知道這是否有助於解決這個問題。

下面顯示用於編輯所述文本視圖的高度的代碼(其是用於選擇器的方法,其將根據接收到的UITextViewTextDidChangeNotification調用):

NSInteger maxInputFieldWidth = self.inputTextField.frame.size.width; 

CGSize maxSize = CGSizeMake(maxInputFieldWidth, 9999); 
CGSize neededSize = [self.inputTextField sizeThatFits:maxSize]; 

NSInteger neededHeight = neededSize.height; 

if (self.inputTextField.hasText) 
{ 
    [self.inputTextField scrollRangeToVisible:NSMakeRange([self.inputTextField.text length], 0)]; 

    if (neededHeight <= TEXTVIEW_MAX_HEIGHT_IN_USE && neededHeight != previousHeight) 
    { 
     previousHeight = neededHeight; 

     CGRect inputTextFieldFrame = self.inputTextField.frame; 
     inputTextFieldFrame.size.height = neededHeight; 
     inputTextFieldFrame.origin.y = TEXTVIEW_ORIGIN_Y; 
     self.inputTextField.frame = inputTextFieldFrame; 
    } 
    else if (neededSize.height > TEXTVIEW_MAX_HEIGHT_IN_USE) 
    { 
     if (!self.inputTextField.scrollEnabled) 
     { 
      self.inputTextField.scrollEnabled = YES; 

      CGRect inputTextFieldFrame = self.inputTextField.frame; 
      inputTextFieldFrame.size.height = TEXTVIEW_MAX_HEIGHT_IN_USE; 
      inputTextFieldFrame.origin.y = TEXTVIEW_ORIGIN_Y; 
      self.inputTextField.frame = inputTextFieldFrame; 
     } 
     else if (neededHeight != previousHeight) 
     { 
      previousHeight = neededHeight; 

      CGRect inputTextFieldFrame = self.inputTextField.frame; 
      inputTextFieldFrame.size.height = TEXTVIEW_MAX_HEIGHT_IN_USE; 
      inputTextFieldFrame.origin.y = TEXTVIEW_ORIGIN_Y; 
      self.inputTextField.frame = inputTextFieldFrame; 
     } 
    } 
} 
+0

也許你可以上傳你用來調整textview大小的代碼? – Tcharni

+0

剛剛編輯的問題,添加了代碼塊來調整textview,self.inputTextField是UITextView –

+0

似乎沒有任何錯誤的代碼。嘗試放置previousHeight = neededHeight;在if(!self.inputTextField.scrollEnabled)循環中,並查看它是否有效。 – Tcharni

回答

2

光輝皮特斯坦伯格已經有了UITextView存在很多問題,並因此實施了大量修復。

His article can be found here with links to his code.

For a direct link to the code, it can be found here, but I recommend reading the post.

+0

我會讓你知道結果,當我嘗試這個,thx! –

+0

它不工作...我thk這個問題是不同於我可以找到使用谷歌的任何其他人。 –

+0

http://stackoverflow.com/questions/20632830/uitextview-with-scroll-dynamically-set-resizes-to-default 我的問題類似於這個,雖然沒有答案 –

3

過了一年之後和scrollEnabled仍然造成問題。我有一個類似的問題,設置scrollEnabled = true(我使用Swift)不會導致任何更改。

我通過在textView的各個邊上設置自動佈局約束來解決問題。然後,就像你在這裏詳細說明的那樣,我只是再次設置textView.frame。我的猜測是,這會導致一些內部更新,實際上會滾動。我也猜測,自動佈局然後強制textView留在正確的高度,而不是你正在經歷的崩潰。

+0

絕對正確,這應該是接受的答案。 只需要添加這個,你**不需要改變框架大小,你可以放這個代碼:'textView.frame = textView.frame' – Ivan

2

我遇到了類似的問題(我使用的自動佈局),並能夠與以下設置來解決這個問題:

  1. 添加頂部,領導,下,後隨區間約束我的文字查看
  2. 添加一個優先級爲999的高於或等於最小高度約束(在我的情況下,它被設置爲50)
  3. 添加一個優先級小於或等於最大高度約束1000(在我的情況下,它被設置爲125)
  4. 添加一個與先驗等高的約束ty 1000(設置爲125),並確保它沒有安裝(在Interface Builder中取消選中'已安裝'選項或在代碼約束中將'active'設置爲NO/false)

然後我使用下面的代碼確定文本視圖的高度和啓用/禁用滾動和約束:

- (void)textViewDidChange:(UITextView *)textView { 
    ... 

    CGSize size = textView.bounds.size; 
    CGSize newSize = [textView sizeThatFits:CGSizeMake(size.width, CGFLOAT_MAX)]; 

    if (newSize.height >= self.textViewMaxHeightConstraint.constant 
     && !textView.scrollEnabled) { 
     textView.scrollEnabled = YES; 
     self.textViewHeightConstraint.active = YES; 
    } else if (newSize.height < self.textViewMaxHeightConstraint.constant 
       && textView.scrollEnabled) { 
     textView.scrollEnabled = NO; 
     self.textViewHeightConstraint.active = NO; 
    } 

    ... 
} 

使用sizeThatFits:來確定文本視圖的所需尺寸,我任一組滾動啓用或禁用。如果啓用,我將高度約束設置爲激活,以強制文本視圖保持在所需的高度。