2012-06-14 59 views
0

我在這裏有一些問題。我有一個UITextView,當我點擊裏面的時候被鍵盤取消,所以,我想在這裏做的是一個UIScrollView,它允許我看到我在TextView中寫的內容...問題與UIScrollView和UIKeyboard

這是到目前爲止我的代碼:

-(void)keyboardWillShow { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView animateWithDuration:.3 animations:^{ 
     CGRect rect = self.view.frame; 
     if (movedUp) 
     { 
      scrollView.contentSize = CGSizeZero; 
      scrollView.frame = CGRectMake(0, 80, 320, 308); 
//   rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
//   rect.size.height += kOFFSET_FOR_KEYBOARD; 
     } 
     else 
     { 
      scrollView.contentSize = CGSizeZero; 
      scrollView.frame = CGRectMake(0, 190, 320, 308); 
//   rect.origin.y += kOFFSET_FOR_KEYBOARD; 
//   rect.size.height -= kOFFSET_FOR_KEYBOARD; 
     } 
     self.view.frame = rect; 
    }]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillShowNotification 
                object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:UIKeyboardWillHideNotification 
                object:nil]; 
} 

的問題是,代碼工作不正常,當我點擊TextView的視圖,而不是往上走,它的股價下跌......

任何想法?

回答

0

UITextView已經是的滾動視圖。當您輸入的文字多於可見文字時,它應該自動滾動。此外,用戶可以隨時自行滾動文本。

+0

不,你不明白的問題,我的問題是,鍵盤解僱的UITextView被調用的時候......我wan't滾動整個的UIView該鍵盤不解僱我的TextView ... – Marnerk

+0

你的意思是鍵盤**是覆蓋**你的文字視圖?對於鍵盤無法消除文本視圖,只能反過來:P。 – Mundi

0

我得到了我所有問題的答案!

- (void)textViewDidBeginEditing:(UITextView *)textView{ 

    [self scrollViewAsd:130]; 

} 

- (void)textViewDidEndEditing:(UITextView *)textView{ 

    [self scrollViewAsd:0]; 

} 

- (void)scrollViewAsd:(float) inset 
{ 

    [UIView animateWithDuration:.3 animations:^{ 
     scrollView.contentInset = UIEdgeInsetsMake(0, 0, inset, 0); 
     scrollView.contentOffset = CGPointMake(0, inset); 

    }]; 
}