2013-07-20 42 views
-1

From Google翻译:Keyboard and TextField

你好,当我点击文本字段时,我对选取器视图做了一些修改。

当我从文本字段导航到文本字段而未触及“完成”按钮时,我转到包含选取器视图的文本字段,并且按下完成后,滚动视图不会恢复到其原始大小。

你知道如何取悦吗?

原文:

卓悦,lorsque JE集团河畔的联合国文本字段,J'AI FAIS quelques修改倒qu'un选择器视图apparaisse。

Quand je navigue de text field en text field sans toucher sur la touche“Terminé”et que je je rend dans un text field qui contient un picker view et que la,j'appuis surterminé,le scroll view ne remand pas sa taille motle。

Savez vous评论faire s'il vousplaît?

Voici周一代码:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [ScrollView setContentSize:CGSizeMake(320, 974)]; 
    [ScrollView setScrollEnabled:YES]; 

    //on abonne notre instance aux notifications du clavier lorsqu'il apparaît et lorsqu'il disparait 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
               name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification object:nil]; 
} 

- (void)keyboardWillShow:(NSNotification *)aNotification { 
    //si le clavier est déjà présent on ne fait rien 
    if (keyboardIsShown) { 
     return; 
    } 

    if (nom.editing == YES) 
    { 
     //on récupère la taille du clavier 
     NSDictionary* info = [aNotification userInfo]; 
     CGRect _keyboardEndFrame; 
     [[info valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame]; 
     CGSize keyboardSize = _keyboardEndFrame.size; 

     //on crée une nouvelle frame pour la scrollView 
     CGRect viewFrame = self.ScrollView.frame; 
     viewFrame.size.height -= keyboardSize.height; 

     //on redimensionne la scrollView dans une animation 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDuration:0.3]; 
     [self.ScrollView setFrame:viewFrame]; 
     [UIView commitAnimations]; 

     //on scroll jusqu'au champs texte en cours d'édition 
     CGRect textFieldRect = nom.frame; 
     [self.ScrollView scrollRectToVisible:textFieldRect animated:YES]; 

     //on enregistre l'état actuel du clavier 
     keyboardIsShown = YES; 
    } 

    else if (dateVol.editing == YES) 
    { 
     //on récupère la taille du clavier 
     NSDictionary* info = [aNotification userInfo]; 
     CGRect _keyboardEndFrame; 
     [[info valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame]; 
     CGSize keyboardSize = _keyboardEndFrame.size; 

     //on crée une nouvelle frame pour la scrollView 
     CGRect viewFrame = self.ScrollView.frame; 
     viewFrame.size.height -= keyboardSize.height; 

     //on redimensionne la scrollView dans une animation 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDuration:0.3]; 
     [self.ScrollView setFrame:viewFrame]; 
     [UIView commitAnimations]; 

     //on scroll jusqu'au champs texte en cours d'édition 
     CGRect textFieldRect = prenom.frame; 
     [self.ScrollView scrollRectToVisible:textFieldRect animated:YES]; 

     //on enregistre l'état actuel du clavier 
     keyboardIsShown = YES; 
    } 
} 

//méthode appelée lorsque le clavier disparaît (on quitte l'édition d'un champs texte) 
- (void)keyboardWillHide:(NSNotification *)aNotification { 

    if (nom.editing == YES || dateVol.editing == YES) 
    { 
     //get the size of the keyboard 
     NSDictionary* info = [aNotification userInfo]; 
     CGRect _keyboardEndFrame; 
     [[info valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame]; 
     CGSize keyboardSize = _keyboardEndFrame.size; 

     //resize the scroll view 
     CGRect viewFrame = self.ScrollView.frame; 
     viewFrame.size.height += keyboardSize.height; 

     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationBeginsFromCurrentState:YES]; 
     [UIView setAnimationDuration:0.3]; 
     [self.ScrollView setFrame:viewFrame]; 
     [UIView commitAnimations]; 

     keyboardIsShown = NO; 
    } 
} 
+3

你能用英文发表你的问题吗? –

+2

Anglais s'il vousplaît(英语请)。 – rmaddy

+0

要么你正在做一件非常奇怪的事情,把'UIPickerView'放到'UITextField'中,否则Google Translate不能很好地工作。我假设后者,并问我的朋友是否可以帮忙。 – WolfLink

回答

0

我想你说你已经设置的UITextField到一个UIPickerView的inputView,而当你点击使用UIPickerView滚动视图做完之后没有返回到其以前的位置。看看你的代码,我想这是因为你的keyboardWillHide方法只在系统发送UIKeyboardWillHideNotification时才被调用。我假定当您按下完成按钮时系统没有发送UIKeyboardWillHideNotification。当您按下完成按钮时,您需要将代码添加到所调用的任何方法中,该按钮将调用您的keyboardWillHide方法。

+0

是的,我认为是这样,但请问这是什么代码?你有好主意吗 ? – user2602992

+1

完成按钮是否会使您的UIPickerView消失?如果是这样的话,那么当发生这种情况时,你应该有一些代码将'resignFirstResponder'发送给你的UITextField。像这样添加 –

+0

:'[myTextField resignFirstResponder];'。正常情况下,用它来使键盘消失。 – WolfLink