2012-02-01 21 views
1

我註冊了鍵盤顯示和隱藏通知。UITextView在更新其框架後不滾動

當文本視圖沒有被點擊時,它可以滾動。即當它不在可編輯模式下。 當用戶點擊..我更新它的框架來.. ..然後它不滾動。 。結束編輯後..我改變立場回其滾動再次..

這裏是我的代碼

- (void)keyboardWillHide:(NSNotification *)n 
{ 
    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 


    // resize the scrollview 
    CGRect viewFrame = self.NoteTextView.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    viewFrame.origin.y += (keyboardSize.height * 0.36); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    // The kKeyboardAnimationDuration I am using is 0.3 
    [UIView setAnimationDuration:0.1]; 

    [UIView commitAnimations]; 

    [self.NoteTextView setContentSize:CGSizeMake(310,580)]; 
    [self.NoteTextView setScrollEnabled:YES]; 

    keyboardIsShown = NO; 
} 

- (void)keyboardWillShow:(NSNotification *)n 
{ 
    // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown. 
    if (keyboardIsShown) { 
     return; 
    } 

    NSDictionary* userInfo = [n userInfo]; 

    // get the size of the keyboard 
    CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 


    // resize the noteView 
    CGRect viewFrame = self.NoteTextView.frame; 
    // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. 
    viewFrame.origin.y -= (keyboardSize.height * 0.36); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    // The kKeyboardAnimationDuration I am using is 0.3 
    [UIView setAnimationDuration:0.3]; 
    [self.NoteTextView setFrame:viewFrame]; 
    [UIView commitAnimations]; 


    [self.NoteTextView setContentSize:CGSizeMake(310, 580)]; 
    [self.NoteTextView setScrollEnabled:YES]; 
    keyboardIsShown = YES; 
} 

我甚至已經啓用滾動啓用並在方法改變其內容的大小。

+0

你是否嘗試過做同樣的事情,除非沒有動畫並查看它是否有效? – KDaker 2012-02-01 12:24:48

+0

是的..我試過..但沒有工作..所以現在我嘗試的解決方案是在工具欄中添加欄按鈕項目,並將其作爲附件視圖添加到文本視圖...所以現在用戶輸入使它可以滾動。 – Shubhank 2012-02-01 14:16:54

+0

好的,儘管我確實相信必須有一種方法可以讓它在沒有輔助視圖的情況下工作。我猜測這個問題與上面的代碼無關。也許它與其他UIViews存在。也許是訂購的一個問題......您在該課程中還有哪些其他UIViews或控件? – KDaker 2012-02-01 15:24:15

回答

1

這聽起來很奇怪。
我的建議:
1.檢查新監視器中其超級用戶的UserInteractioEnable屬性是否爲非假
2.在新建且明確的項目上嘗試此代碼。

+0

檢查超級視圖上的'userInteractionEnabled'屬性很重要。它解決了我遇到的類似問題。 – 2013-06-25 06:09:49