2012-09-14 61 views
0

在我的應用程序中,我使用的UIScrollViewUITextFieldUITextView,我做了UITextField中鍵盤方向的編碼。它工作正常,但它不適用於UITextView。在編輯UITextView時,鍵盤完全隱藏了UITextView。這裏是我的代碼在UITextView中的UIScrollView的鍵盤方向

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    [textField setClearButtonMode:UITextFieldViewModeWhileEditing]; 
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 
    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 
    if (heightFraction < 0.0) { 
    heightFraction = 0.0; 
    } else if (heightFraction > 1.0) { 
    heightFraction = 1.0; 
    } 
    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationPortrait || 
    orientation == UIInterfaceOrientationPortraitUpsideDown) { 
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    } else { 
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
    } 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y -= animatedDistance; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 
} 
- (void)textFieldDidEndEditing:(UITextField *)textField { 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y += animatedDistance; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations];  
} 

任何人都可以幫我清楚這一點嗎?

回答

1

使用委託方法的TextView爲UITextView的 像波紋管

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

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

同樣喜歡textField委託方法

比如...

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

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    self.view.frame=CGRectMake(0, -100, 320, 460);//just for an example 
    [UIView commitAnimations]; 

} 

-(void)textViewDidEndEditing:(UITextView *)textView{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    self.view.frame=CGRectMake(0,0, 320, 460); 
    [UIView commitAnimations]; 
} 

希望這有助於你..

:)

+0

也在這些委託方法中定義了.h文件中的UITextFieldDelegate –

+0

' - (void)textViewDidBeginEditing:(UITextView *)textView; '和' - (void)textViewDidEndEditing:(UITextView *)textView; ' 我應該怎麼辦 – iosdev

+0

你在文本框使用的所有代碼使用的代碼,這些方法只是使用關鍵字「的TextView」而不是「文本框」 –

0

首先在加入你的 '.H' 文件

做負載

textView.delegate=self; 

然後 給出textView代碼

textView.userinteractionEnabled=YES;