2013-06-28 22 views
1

我有問題在底部我的UITextview上移。Textview無法向上移動時開始編輯

UITextField工作正常,但UITextView不起作用。

這是我的代碼,當按下TextView時,它彈出工具欄。請幫助

- (IBAction)HideKeyboard:(id)sender { 
    [self.myTextField resignFirstResponder]; 
    [self.myTextView resignFirstResponder]; 
    [self.myScrollView setContentOffset:CGPointZero animated:YES]; 
} 
+0

self.myTextView.delegate = YES?請檢查一下..你可能錯過了。 – Bala

+0

不,我已經設置textview委託。你知道爲什麼嗎? –

+0

它應該是'[self.myTextView setContentOffset:CGPointZero animated:YES];'而不是? –

回答

0

您的TextView必須位於ScrollView下。

試試這個。

在viewDidLoad方法鍵盤定通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil]; 

-(void)keyboardDidShow:(NSNotification *)notif 
{ 

scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+350); 
    NSTimeInterval duration = 0.6; 

if(txt_preRemindText.isFirstResponsder) 
    { 
     [UIView animateWithDuration:duration animations: 
     ^{ 
      [scrollView setContentOffset:CGPointMake(0,260) animated:YES]; 

     }]; 

    } 

} 
1

我假設你需要移動UITextView中的鍵盤上方,然後試試下面的辦法

添加TPKeyboardAvoiding Framework到您的項目,該項目將採取小心移動鍵盤上方的UITextField'sUITextView's

+0

@ jason-lau如果您發現任何**有用的答案,請**提高它**,如果答案是正確的,那麼請**接受**這將有助於從**清單中刪除問題未回答問題列表** – icodebuster

0

使用此代碼。

//此代碼添加到導入文件的下面。

static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 

//然後添加此功能。

-(void) textViewDidBeginEditing:(UITextView *)textView 

{

CGRect textFieldRect = [self.view.window convertRect:textView.bounds fromView:textView]; 
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]; 
    } 

//並添加TextView的

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

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]; 
} 


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 
if ([text isEqualToString:@"\n"]) 
{ 
    [txt_description resignFirstResponder]; 
    return NO; 
} 
return YES; 

這種方法}

你的看法,如果你在TextView的編輯。

相關問題