2013-08-14 9 views
16

कैसे स्थानांतरित करें UITextView कुछ मूल्य दर्ज करना प्रारंभ करते समय ऊपर और नीचे। टेक्स्टफिल्ड की तरह हम इसकी प्रतिनिधि विधि का उपयोग करते हैं। UITextView के मामले में क्या करना है?कुंजीपटल मौजूद होने पर UITextView को कैसे स्थानांतरित करें

+0

'textFieldDidBecomeActive'> तदनुसार टेक्स्ट फ़ील्ड को ले जाएं। –

+0

http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present कृपया यह लिंक देखें –

+1

क्या खोज करना मुश्किल है? यहां तक ​​कि यदि यह है, साइडबार देखें: यह आपको संबंधित प्रश्न दिखाता है। – Abizern

उत्तर

19

संपादन पूरा दृश्य मैं सुझाव है कि आप this guide पर एक नज़र लेने के लिए

- (void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    [self animateTextView: YES]; 
} 

- (void)textViewDidEndEditing:(UITextView *)textView 
{ 
    [self animateTextView:NO]; 
    } 

- (void) animateTextView:(BOOL) up 
    { 
     const int movementDistance =heightKeyboard; // tweak as needed 
     const float movementDuration = 0.3f; // tweak as needed 
     int movement= movement = (up ? -movementDistance : movementDistance); 
     NSLog(@"%d",movement); 

     [UIView beginAnimations: @"anim" context: nil]; 
     [UIView setAnimationBeginsFromCurrentState: YES]; 
     [UIView setAnimationDuration: movementDuration]; 
     self.view.frame = CGRectOffset(self.inputView.frame, 0, movement); 
     [UIView commitAnimations]; 
    } 

मुझे आशा है कि यह आप में मदद मिलेगी ...

+2

हम, आपको एनीमेशन के लिए ब्लॉक का उपयोग करना चाहिए, आईओएस 4 – Beuj

+1

के बाद से आपकी विधि को निराश किया गया है, मुझे इस कोड के साथ बहुत से मुद्दों का सामना करना पड़ा। 'self.inviewView.frame' को 'self.view.frame' के साथ बदलकर हल किया गया। – Rajesh

+0

जब आप स्क्रॉलव्यू के साथ समाधान का उपयोग नहीं कर सकते हैं, तो यह सबसे आसान और सबसे अच्छा तरीका है –

2
#define kOFFSET_FOR_KEYBOARD 80.0 

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    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)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

इस कोड की कोशिश .....

+0

वहाँ एक ही चीज़ पर एक सवाल है .. –

+2

Annnnnddd आपको एनीमेशन के लिए ब्लॉक का उपयोग करना चाहिए, आपकी विधि आईओएस 4 के बाद से निराश है ... – Beuj

+0

यह टेक्स्ट फ़ील्ड का जवाब है, टेक्स्ट व्यू नहीं। – Shmidt

3

मैं फ्रेम बदलकर किया था।

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

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES]; 
viewMsg.frame = CGRectMake(10, 50, 300, 200); 
[UIView commitAnimations]; 

NSLog(@"Started editing target!"); 

} 

-(void)textViewDidEndEditing:(UITextView *)textView 
{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:viewMsg cache:YES]; 
viewMsg.frame = CGRectMake(10, 150, 300, 200); 
[UIView commitAnimations]; 
} 
5

यहां एक नमूना कोड है जो आपके लिए स्वचालित रूप से कीबोर्ड को संभालेगा। Keyboard avoiding

आप tableview उपयोग कर रहे हैं तो अपने tableView TPKeyboardAvoidingTableView का एक उपवर्ग होना चाहिए और यदि आप scrollview उपयोग कर रहे हैं तो अपने scrollview TPKeyboardAvoidingScrollView का एक उपवर्ग होना चाहिए। और यह लाइब्रेरी स्वचालित रूप से आपके लिए कीबोर्ड हैंडलिंग करेगी।

4

ऊपर आ जाएगा और पूर्ण संपादन के बाद नीचे ले जाने के करते हैं ...

विशेष रूप से अनुभाग: कुंजीपटल के तहत स्थित सामग्री को स्थानांतरित करना। मैंने इस दृष्टिकोण का सफलतापूर्वक कई बार उपयोग किया है।

संबंधित मुद्दे