2015-03-19 83 views
0

我按照Apple文檔在鍵盤出現時向上移動文本框。 代碼工作正常我的問題是,我需要一個特定的文本字段向另一個移動,而不是執行代碼蘋果每個我選擇的文本字段向上移動...我怎樣才能移動一個特定的文本字段,而不是所有?鍵盤顯示時移動特定的UITextField

非常感謝你,我插入下面的代碼使用

-(void)viewWillAppear:(BOOL)animated 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

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

} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification { 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    CGRect bkgndRect = changePasswordTextField.superview.frame; 
    bkgndRect.size.height -= kbSize.height; 
    [scrollView setContentOffset:CGPointMake(0.0, changePasswordTextField.frame.origin.y+kbSize.height) animated:YES]; 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification { 


    [scrollView setContentOffset:CGPointZero animated:YES]; 
} 

回答

0

您可以通過以下步驟實現自己的功能。

  1. 設置你的UITextField的委託。
  2. 執行textFieldDidBeginEditing當鍵盤打開文本字段時將調用該方法。所以你可以在這個方法中改變文本框的框架如下。

    -(void)textFieldDidBeginEditing:(UITextField *)textField{ 
        [textField setFrame:CGRectMake(0.0, textField.frame.origin.y-VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES]; 
        // VALUE = textfield you want to move upward vertically 
    } 
    
  3. 現在,來處理鍵盤隱藏事件,您可以在textFieldDidEndEditing方法,在下面設置文本框的邊框到它的起源。

    - (void)textFieldDidEndEditing:(UITextField *)textField{ 
         [textField setFrame:CGRectMake(0.0, textField.frame.origin.y+VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES]; 
         // VALUE = textfield you want to move downward vertically 
    } 
    

我希望它可以幫助你。